如何在应用程序目录中创建Application.exe.config
因此,我最近更新了我的应用程序以支持新功能。过去如果配置文件被用户删除了也没什么大不了的。这项新功能要求它存在,其中一个要求是该文件存在于应用程序的安装目录中。
我注意到当文件被删除时(取决于我尚未弄清楚的变量),我收到一条 .NET 通知,表明配置文件丢失或损坏。目前我的程序崩溃了(我仍然必须弄清楚如何复制此行为),这就是这个问题的原因。
我熟悉配置管理器。加载默认值后,我在写入文件时遇到问题。由于某种原因强制保存似乎不会重新创建文件,至少不会在安装目录中重新创建文件,这是必需的。
我正在寻找有关如何以优雅的方式处理这种极端情况的指导。我会发布代码,老实说,这只是所有失败的尝试,虽然我的尝试确实生成了一个文件,但内容不是我正在寻找的设置。
我愿意发布任何可能有帮助的内容。
So I recently updated my application to support a new feature. In the past if the configuration file was deleted by the user it wasn't a big deal. This new feature requires it to exist, and one of the requirements is that, the file exists in the application's installation directory.
I have notice when the file is deleted ( depending on variables I have not figured out ) I get a .NET notification that the configuration file is missing or corrupt. Currently my program then crashes ( I still have to figure out how to duplicate this behavior ) which is the reason for this question.
I am familar with ConfigurationManager. I am having trouble writting the file once the default values are loaded. Forcing a Save for some reason does not seem to recreate the file, at least not in the installation directory, which is a requirement.
I am looking for guidence on how to handle this corner case in an elegant manner. I would post code, honestly its just all failed attempts, which while my attempts do generate a file the contents are not the settings I am looking for.
I am willing to post anything that might be able to help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不知道。在正常情况下,程序无法写入其安装目录 - 这是一个标准的 Windows 安全问题,也是应用程序应用程序数据应驻留在外部(从 exe 的角度来看)目录的原因。
如果管理员删除配置文件,则会崩溃,要求重新安装。您无法可靠地执行任何操作,因为您不能假设您可以在运行时写入该文件夹。一条消息后跟一个事件日志条目是最好的方法。用户不应删除应用程序的部分内容。
You dont. Under normal conditions the program can not write into it's install directory - this is a standard windows security issue and the reason why app application data should reside ni external (from the exe's point) driectories.
If an admin deletes the config file, crash, ask for reinstall. There is nothing you can RELIABLY do, as you can not assume you can write into the folder at runtime. A message followed by an event log entry is the best approach here. Users are not supposed to delete parts of the application.
停止使用内置配置支持,只需使用标准 XML 类对名为 Something.exe.config 的文件进行写入/读取操作,如果该文件被删除,只需根据可执行文件中硬编码的值重新创建它即可。
配置文件支持应该让事情变得更容易,如果你需要做一些让事情变得困难的事情,不要使用它。
Stop using the built-in config support and just use write/read to a file called something.exe.config using the standard XML classes and if that gets deleted, just re-create it from values hard-coded in the executable.
The config file support is supposed to make things easier, if you need to do stuff where it makes things difficult, don't use it.
类似于
var wcfm = new WebConfigurationFileMap();
配置 newConfig = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
newConfig.Save();
不起作用?
Something like
var wcfm = new WebConfigurationFileMap();
Configuration newConfig = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
newConfig.Save();
doesn't work?