.NET ApplicationSettings - 从哪里加载设置?
我的设置最初属于用户设置。我注意到与程序集位于同一目录中的主 .config 被 .NET 忽略。如果我更改任何设置,它在运行时仍然会以默认值结束。我无法在 User AppData 中找到配置文件的副本。我将设置的范围更改为应用程序,但 .NET 仍然忽略配置文件。
它尝试加载的文件在哪里?如何让它只使用程序集目录中的 .config 文件?
I have settings that were originally scoped as User settings. I notice that the main .config that lives in the same directory as the assembly is being ignored by .NET. If I change any setting it still always ends up with the default values at run-time. I was unable to find a copy of the config file in User AppData. I changed the settings' scope to Application and yet .NET is still ignoring the config file.
Where is the file it's trying to load instead? How can I get it to only use the .config file in the assembly's directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Visual Studio 运行应用程序,则每次完成重新编译时 Visual Studio 都会重新生成设置文件。
因此,如果您更改应用程序中的设置并重建并重新构建,在 Visual Studio 中启动,设置文件将恢复为 Visual Studio 项目中存储的值。
If you are using visual studio to run your application, visual studio regenerates the settings file every time you complete a recompile.
So if you change your settings within the application and rebuild & launch within visual studio, the settings file will revert to what values are stored in the visual studio project.
如果您使用配置管理器从配置文件读取设置,它必须从 appname.exe.config 文件加载数据。尝试此代码(您必须添加对 system.configuration 程序集的引用)
If you use Configuration manager to read settings from config file, it has to load data from appname.exe.config file. Try this code (you have to add reference to system.configuration assembly)