.net 桌面应用程序 - 如何存储/更改应用程序设置或连接字符串?
我需要让用户能够更改应用程序设置,在本例中是应用程序数据库的位置。我注意到应用程序设置在运行时是只读的,但这需要是应用程序范围的,而不是特定于用户的。如何在 windows.forms 中保留可在运行时更改的应用程序范围连接字符串?
I need to give the user the ability to change application settings, in this case the location for the application database. I noticed that Application Settings are read only at run time, but this needs to be application-wide, not user-specific. How do I persist an application-wide connection string in windows.forms that is changeable at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用
您还
ConnectionStringSettings.ConnectionString 属性
也可以设置。
You can use
Also
ConnectionStringSettings.ConnectionString Property
Can be set too.
框架中没有使用较新的设置类的直接方法 - 您可以通过设置支持保存用户级设置,但不能保存应用程序/机器级别。最简单的方法是 XmlSerialize 一个类并将其存储在一个目录中,该目录在所有具有写入权限的用户之间共享,例如公共文档文件夹(不应将程序目录写入),例如。
Environment.SpecialFolder.CommonApplicationData
编辑:是的,旧的 appSettings 机制有一种编写方式,但这是坏消息 - 新的设置内容忽略了这个设施,因为它是坏消息,有很多原因要尝试写入程序文件中的配置文件
There isnt a direct way in the framework using the newer Settings classes - you can save user-level settings with the Settings support, but not application/machine level. Easiest way is to XmlSerialize a class and store it in a directory that is shared between all users with write acccess when they are running un-elevated such as the Public Documents folder (the program directory shouldnt be written to), e.g..
Environment.SpecialFolder.CommonApplicationData
EDIT: Yes, the old appSettings mechanism has a way of writing, but that's bad news - the newer Settings stuff omits this facility as it is bad news for lots of reasons to try to write to config files in Program Files