如何在 Visual C# 中使用设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/650555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:48:27  来源:igfitidea点击:

How to use settings in Visual C#

c#vb.netsettings

提问by Omar Abid

I have been a VB.net developer for a long time and changed recently to C#. When I was using VB.net, to call user settings, I just need to create a new setting using the designer and then call it from code with the My namespace.
Here's the code
My.settings.anysetting
I can change or get data from it. However in C# the My keyword doesn't work, so what can I do to call settings??

我长期从事 VB.net 开发,最近改用 C#。当我使用 VB.net 调用用户设置时,我只需要使用设计器创建一个新设置,然后使用 My 命名空间从代码中调用它。
这是
My.settings.anysetting
我可以更改或从中获取数据的代码。但是在 C# 中 My 关键字不起作用,那么我该怎么做才能调用设置??

采纳答案by Samuel

Settingsare stored under the Applicationfolder and as such, use that as their namespace.

Settings存储在Application文件夹下,因此,将其用作其命名空间。

int myInt = Properties.Settings.Default.myVariable;
Properties.Settings.Default.myVariable = 12;
Properties.Settings.Default.Save();

Using Settings in C#

在 C# 中使用设置

回答by Andrew

// Retrieving connection string from Web.config.
String connStringMSQL = WebConfigurationManager.ConnectionStrings["andi_msql"].ToString();

In my case this was for my connectionStrings setting but depending on what node your setting is in you can change this accordingly.

在我的情况下,这是我的 connectionStrings 设置,但根据您的设置所在的节点,您可以相应地更改它。

Hope this helps

希望这可以帮助

回答by C-Cat

Kinda like this:

有点像这样:

this.BackColor = Properties.Settings.Default.myColor;

Documentation: https://msdn.microsoft.com/en-us/library/bb397759(v=vs.110).aspx

文档:https: //msdn.microsoft.com/en-us/library/bb397759(v=vs.110).aspx