无法修改c:\ windows目录中的.ini文件 [英] Unable to modify .ini file in c:\windows directory
问题描述
我正在编写C#Windows应用程序以更新旧版应用程序的.ini文件.我没有旧版应用程序的源代码,因此无法对其进行修改.
I'm writing a C# Windows application to update a .ini file for a legacy application. I don't have the source for the legacy application, so I cannot modify it.
旧版应用程序将设置存储在C:\ Windows的INI文件中.此位置无法更改.
The legacy application stores settings in a INI file in C:\Windows. This location cannot be changed.
要修改INI设置,我一直在使用以下Windows功能:
To modify the INI settings I've been using these Windows functions:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
private static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
上面的函数似乎没有修改INI文件,但是随后对"GetPrivateProfileString"的调用将显示正确/修改后的值.
The above functions don't seem to modify the INI file, however subsequent calls to "GetPrivateProfileString" show the correct/modified values.
此外,我什至尝试使用System.IO类直接写入INI文件.这似乎没有修改INI文件.
Additionally I've even tried writing directly to the INI file using System.IO classes. This didn't seem to modify the INI file.
如果我尝试将文件复制到应用程序的目录中,然后写入INI设置,则会写入正确的值.如果随后我尝试将此修改后的文件复制回C:\ Windows,则原始INI文件似乎没有被修改.
If I try to copy the file to the application's directory then write the INI settings, the correct values are written. If I then try to copy this modified file back to C:\Windows the original INI file doesn't seem get modified.
似乎在操作系统级别正在发生某种类型的INI文件缓存?
It seems like there's some type of INI file cache happening at the OS level?
如果我使用文本编辑器手动编辑INI文件,则会更新INI文件.
If I manually edit the INI file using a text editor the INI file is updated.
正如在其他论坛上所建议的那样,在编写INI设置后,我也尝试了以下代码:
As suggested on other forums I've also tried this code after writing the INI setting:
WritePrivateProfileString(null, null, null, filename);
我的问题是,旧版应用程序无法获取INI文件更改(即使在重新启动旧版应用程序之后).
My problem is that the legacy app doesn't pick up the INI file changes (even after restarting the legacy app).
推荐答案
在UAC下,没有UAC的应用程序会尝试执行UAC阻止的操作,因此需要进行虚拟化.这将写入镜像位置而不是受保护的位置.尝试读取时,如果镜像位置中有文件,则会得到该文件.这样,即使编写的应用与当时的最佳实践相矛盾,它也可以正常工作.
Under UAC, applications with no UAC manifest that try to do things UAC prevents are subject to virtualization. This writes to a mirror location instead of the protected location. When you try to read, if there is a file in the mirror location, you get that file. In this way your app will work, even though it was written contradictory to the best practices of the time.
要查找镜像位置,请在文件夹中打开Windows资源管理器,然后在工具栏中查找兼容文件"按钮:
To find the mirror location, open a Windows explorer to the folder and look for a button in the toolbar that says Compatibility Files:
这也许对您已经足够了-现在您知道了外观,您可以信任虚拟化,并且该应用程序将正常运行.如果没有,请考虑在应用程序上放置一个外部清单.带有 asInvoker
的清单将阻止虚拟化,并且写入将失败.如果应用程序有错误处理(例如回退到另一个位置),则可能很好.带有 requireAdministrator
的清单将阻止虚拟化,并且写入将成功.但是,UAC的同意可能会惹恼您的用户.
Perhaps this is enough for you - now that you know where to look you can trust virtualization and the app will work properly. If not, consider putting an external manifest on the application. A manifest saying asInvoker
will prevent virtualization and the writes will fail. If the app has error handling (like falling back to another location) perhaps this is good. A manifest saying requireAdministrator
will prevent virtualization and the writes will succeed. However the UAC consent will probably annoy your users.
(图像,btw,来自我的一个博客条目,该条目更多地讨论了虚拟化.
(The image, btw, is from a blog entry of mine that talks a bit more about virtualization. http://www.gregcons.com/KateBlog/FindingFilesYoureSureYouWrote.aspx)
我发现有很多应用程序,以管理员身份运行一次即可将我的设置设置到位,然后我就可以运行而无需提升.在这种情况下,请训练您的用户右键单击,以管理员身份运行.
I find with many apps, a single run as administrator gets my settings in place and after that I can run without elevating. In that case train your users to right-click, run as administrator.
这篇关于无法修改c:\ windows目录中的.ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!