.net 桌面应用程序 - 如何存储/更改应用程序设置或连接字符串?

发布于 2024-08-12 01:02:20 字数 123 浏览 8 评论 0原文

我需要让用户能够更改应用程序设置,在本例中是应用程序数据库的位置。我注意到应用程序设置在运行时是只读的,但这需要是应用程序范围的,而不是特定于用户的。如何在 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 技术交流群。

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

发布评论

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

评论(2

滴情不沾 2024-08-19 01:02:21

可以使用

ConfigurationManager.AppSettings.Set()

您还

ConnectionStringSettings.ConnectionString 属性

也可以设置。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("test","tada");
config.Save(ConfigurationSaveMode.Minimal, true);

You can use

ConfigurationManager.AppSettings.Set()

Also

ConnectionStringSettings.ConnectionString Property

Can be set too.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("test","tada");
config.Save(ConfigurationSaveMode.Minimal, true);
娇女薄笑 2024-08-19 01:02:21

框架中没有使用较新的设置类的直接方法 - 您可以通过设置支持保存用户级设置,但不能保存应用程序/机器级别。最简单的方法是 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

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