c# Settings.settings 设置

本文介绍了如何在C#中使用Properties.Settings进行配置文件的定义、读取和修改操作,并详细解释了不同作用域配置的区别。

使用Properites中的Settings可以很方便的添加配置
  具体配置如下图:

c# <wbr>Settings.settings <wbr>设置
  读取配置值
  

1 String strAddress = Properties.Settings.Default.Address;

 


  修改和保存配置(只针对用户配置)
  

Properties.Settings.Default.Address= "Address";
  Properties.Settings.Default.Save();//使用Save方法保存更改

 


  根据配置的Name获取Value
  String strAddress = Properties.Settings.Default.Properties["Address"].DefaultValue
  其中,Address大小写均可

 

1、定义

在Settings.settings文件中定义配置字段。把作用范围定义为:User则运行时可更改,Applicatiion则运行时不可更改。可以使用数据网格视图,很方便;

2、读取配置值

text1.text = Properties.Settings.Default.FieldName;
//FieldName是你定义的字段

 

3、修改和保存配置

Properties.Settings.Default.FieldName = "server";

Properties.Settings.Default.Save();//使用Save方法保存更改

 

注意:当设置scope为User时他的配置放在 C:\Documents and Settings\LocalService\Local Settings\Application Data\在这个目录下或子目录user.config 配置文件中。

转载于:https://www.cnblogs.com/pyffcwj/p/3318441.html

This new 7th edition of Pro C# 6.0 and the .NET 4.6 Platform has been completely revised and rewritten to reflect the latest changes to the C# language specification and new advances in the .NET Framework. You'll find new chapters covering all the important new features that make .NET 4.6 the most comprehensive release yet, including:, A Refined ADO.NET Entity Framework Programming ModelNumerous IDE and MVVM Enhancements for WPF Desktop DevelopmentNumerous updates to the ASP.NET Web APIs, This comes on top of award winning coverage of core C# features, both old and new, that have made the previous editions of this book so popular. Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL opcodes and emitting dynamic assemblies)., The mission of this book is to provide you with a comprehensive foundation in the C# programming language and the core aspects of the .NET platform plus overviews of technologies built on top of C# and .NET (ADO.NET and Entity Framework, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), ASP.NET (WebForms, MVC, WebAPI).). Once you digest the information presented in these chapters, you’ll be in a perfect position to apply this knowledge to your specific programming assignments, and you’ll be well equipped to explore the .NET universe on your own terms., What You Will Learn:, Be the first to understand the .NET 4.6 platform and C# 6.Discover the ins and outs of the leading .NET technology.Learn from an award-winning author who has been teaching the .NET world since version 1.0.Find complete coverage of XAML, .NET 4.6 and Visual Studio 2015 together with discussion of the new Windows Runtime.
`Properties.Settings`是C#中用于保存和获取应用程序设置的类。使用它可以轻松地保存应用程序的配置信息,如上次打开的文件路径、用户偏好设置等。以下是一个简单的示例。 首先,在Visual Studio中打开你的项目,然后右键点击项目,在弹出的上下文菜单中选择“属性”。 选择“设置”选项卡,可以看到一个默认的设置组(例如“Settings1”)。你可以在这里添加和修改应用程序的设置。例如,你可以添加一个名为“LastFolderPath”的字符串设置,用于保存上次打开的文件夹路径。 ```csharp namespace MyApplication { static class Program { static void Main() { // 读取上次保存的文件夹路径 string folderPath = Properties.Settings.Default.LastFolderPath; // 执行其他操作 // ... // 保存应用程序设置 Properties.Settings.Default.Save(); } } } ``` 在上面的示例中,我们使用`Properties.Settings.Default`来读取和保存应用程序的设置。在读取设置时,我们使用`LastFolderPath`属性来获取上次保存的文件夹路径。在保存设置时,我们使用`Save()`方法将更改保存到应用程序的配置文件中。 请注意,`Properties.Settings`类的默认实例是只读的。如果需要在其他类中访问应用程序设置,则可以创建一个新的`Properties.Settings`实例并使用`Default`属性将其初始化。例如: ```csharp Properties.Settings appSettings = new Properties.Settings(); string folderPath = appSettings.LastFolderPath; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值