如何在 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
How to use settings in Visual C#
提问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 codeMy.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
Settings
are stored under the Application
folder 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();
回答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