使用WritePrivateProfileString写入路径问题

发布于 2025-01-07 20:50:34 字数 285 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2025-01-14 20:50:34

我猜想某些东西正在更改您的进程的工作目录,很可能是您的进程中的代码。请注意,文档 有这样说:

如果 lpFileName 参数不包含文件的完整路径和文件名,WritePrivateProfileString 将在 Windows 目录中搜索该文件。如果该文件不存在,该函数将在 Windows 目录中创建该文件。

现在我的猜测是,如果您仅提供文件名,则这适用。因为您的文件名以 . 开头,我相信这将强制该函数从当前工作目录启动。

说了这么多,无论问题的原因是什么,您都应该使用完全限定的路径,以确保文件写入您想要写入的位置。每当您希望文件进入特定目录时,通过使用完全限定路径来强制执行总是最简单的。

您可以使用 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:

If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.

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.

蘑菇王子 2025-01-14 20:50:34

根据 David Heffernan 的回答,您可以使用它

Path.GetDirectoryName(Application.ExecutablePath);

来安全地获取正在运行的应用程序的文件夹部分。

如果您使用的是 dll 而不是可执行文件,则可以使用

Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).CodeBase);

Both require System.IO,并且最初发布于 此处。第二个示例也需要 System.Reflection)。

Further to David Heffernan's answer - you can use

Path.GetDirectoryName(Application.ExecutablePath);

to safely get just the running application's folder part.

If you're in a dll rather than an executable, you can use

Path.GetDirectoryName(Assembly.GetAssembly(typeof(MyClass)).CodeBase);

Both require System.IO, and were originally posted here. Second example also requires System.Reflection).

豆芽 2025-01-14 20:50:34

应用程序数据文件应该写入 LocalApplicationData 特殊文件夹。

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);

您通常没有写入 Program Files 文件夹等的权限。

Application data files are supposed to be written to the LocalApplicationData special folder.

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);

You typically will not have permissions to write into the Program Files folder etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文