在 C# 中存储应用程序设置
在 C# 中存储应用程序设置(例如用户名和密码、数据库位置...)的最佳实践是什么?
提示:我是 .net 和 C# 的新手
What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
应用程序范围内的应用程序配置设置(非用户特定)属于 app.config(对于桌面应用程序)或 web.config(对于 Web 应用程序)。
加密 web.config 文件的各个部分非常简单,如 这个超级简单的例子。
如果您需要存储用户特定设置(如应用程序设置等)或与应用程序配置无关的应用程序范围设置,您可以使用设置文件,如下所述:
C# 中的用户设置
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
我不确定它是在哪个版本的 .net/Visual Studio 中引入的,但您可以右键单击您的项目,选择“添加新项目”,然后从“添加新项目”窗口中选择“设置文件”。这为您的项目提供了一个(默认命名)
Settings.settings
文件,您可以在其中配置要公开的所有设置。您可以将创建的设置定义为 Application< /em> 或 User 这意味着您可以使用这个单一界面来控制全局和用户设置。使用 Visual Studio 提供的编辑器在 Settings.settings 文件中创建设置后,您可以通过如下代码访问它:
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default)
Settings.settings
file that you can configure all the settings you want to expose in.You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
第一个选项是注册表。这很简单,但对于密码来说不太安全。另一种选择是使用您创建的文件。这也不安全,除非您想实施加密。
下一个选项是使用应用程序设置。这也很简单,但有一些问题。首先,右键单击您的项目并转到“属性”。在那里,在“设置”选项卡下,您可以存储可以从程序访问的变量,
您也可以以相同的方式更改它们,但前提是范围设置为“用户”。当范围是应用程序范围时,VS 不允许您出于某种奇怪的原因更改这些变量。要保存更改,您必须按如下方式调用 Save():
这些保存在 C:\Documents and Settings\'Current User'\Local Settings\Application Data\ 下的 User Data 文件夹中
另一种选择是将它们包含在数据库中,但由于您还存储了数据库位置,因此这可能不适合您。
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
我认为app.config(非网络应用程序)或web.config(网络应用程序)。
I think app.config (non web app) or web.config (web app).
此类设置通常位于应用程序配置文件(web.config、app.config)中。
http://www.devasp.net/net/articles/display/679。 html
如果您要存储密码,您可能还需要加密相关的配置部分。
http://msdn.microsoft.com/en-us/library/53tyfkaw。 aspx
http://msdn.microsoft.com/en-us /library/ff650304.aspx
请注意,如果您使用 app.config,您将看到它被重命名为 ..config,具体取决于您的输出是生成 DLL 还是 EXE。
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
正如上面的回复所建议的,app.config 或 web.config 是应用程序设置的最佳位置。
如果您需要更强大的数据库、服务器设置等 xml 样式标记方式,您可以使用configurationSection 并创建自定义部分。
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
对于数据库密码,我的做法是在 xml 标记值中包含一个加密字符串,然后在读取它们时解密,这样就不会暴露密码。
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings 配置文件、ini 文件(nini)、可嵌入数据库(sqlite、berkelydb/etc..),无论您喜欢什么方法,这取决于您的应用程序大小/性能考虑和设计。
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.