使用WritePrivateProfileString写入路径问题
我在 c# 中使用 WritePrivateProfileString (通过 DllImport)来存储从界面上的文本框获取的路径。并且 .ini 文件名在我的应用程序中被硬编码。
string ini_file = ".\\config.ini";
但是,当发生文件写入时,配置文件被写入从接口获取的第一个路径,而不是写入 exe 目录。这很奇怪。 调试显示值已正确发送到 WritePrivateProfileString,但仍然写入到错误的位置。任何人都知道为什么会发生这种情况?
I am using WritePrivateProfileString in c# (through DllImport) to store paths taken from textboxes on the interface. And the .ini file name is hardcoded in my application
string ini_file = ".\\config.ini";
However, when the file writing happens, the configuration file is written to the first path taken from the interface instead of writing it to the exe directory. Which is quite odd.
Debugging shows that the values are sent correctly to the WritePrivateProfileString but it still is written to the wrong location. Anyone knows why is that happenening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜想某些东西正在更改您的进程的工作目录,很可能是您的进程中的代码。请注意,文档 有这样说:
现在我的猜测是,如果您仅提供文件名,则这适用。因为您的文件名以
.
开头,我相信这将强制该函数从当前工作目录启动。说了这么多,无论问题的原因是什么,您都应该使用完全限定的路径,以确保文件写入您想要写入的位置。每当您希望文件进入特定目录时,通过使用完全限定路径来强制执行总是最简单的。
您可以使用
Application.ExecutablePath
找到可执行文件的路径,然后删除文件名部分。另一点需要指出的是,与可执行文件相同的目录可能是一个糟糕的选择。如果您的程序安装在 Program Files 目录下,则包含可执行文件的目录通常不可写。我认为您应该考虑使用用户配置文件中的目录。查找
Environment.SpecialFolder
值之一。I'd guess that something is changing the working directory of your process, most likely your code in the process. Note that the documentation has this to say:
Now my guess is that this applies if you supply just a file name. Because your file name starts with
.
I believe that will force the function to start from the current working directory.Having said all of that, and no matter what the cause of the problem is, you should use a fully-qualified path in order to make sure the file is written where you want it to be written. Whenever you want the file to go in a specific directory, it's always easiest to force that by using fully-qualified paths.
You can find the path to your executable using
Application.ExecutablePath
and then remove the file name part.Another point to make is that the same directory as the executable may be a bad choice. If your program is installed under the Program Files directory then the directory which contains the executable will not be generally writeable. I think you should consider using a directory under in the user profile. Look for one of the
Environment.SpecialFolder
values.根据 David Heffernan 的回答,您可以使用它
来安全地获取正在运行的应用程序的文件夹部分。
如果您使用的是 dll 而不是可执行文件,则可以使用
Both require
System.IO
,并且最初发布于 此处。第二个示例也需要 System.Reflection)。Further to David Heffernan's answer - you can use
to safely get just the running application's folder part.
If you're in a dll rather than an executable, you can use
Both require
System.IO
, and were originally posted here. Second example also requiresSystem.Reflection
).应用程序数据文件应该写入 LocalApplicationData 特殊文件夹。
您通常没有写入 Program Files 文件夹等的权限。
Application data files are supposed to be written to the LocalApplicationData special folder.
You typically will not have permissions to write into the Program Files folder etc.