在.net2.0中,Project级的resource存在于Solution Explorer的Proertires目录下有一个
名为Setting.settings的文件.在此处声明用户配置会在Setting.Designer.cs中生成相关
代码,读写用户配置:
The developer can enter setting information directly into Visual Studio, choosing a name, scope (User/Application), type and optional value. Strongly-typed classes are generated that provide programmatic access to the settings, as well as load and save operations that manipulate settings in the user’s Local Settings\Application Data folder.
private void LoadRecentUrls()
{
String recentUrlsXml = Properties.Settings.Default.RecentUrls;
if (!string.IsNullOrEmpty(recentUrlsXml))
{
XmlSerializer xs = new XmlSerializer(typeof(List<UrlInfo>));
List<UrlInfo> urlInfos = (List<UrlInfo>)xs.Deserialize(new StringReader(recentUrlsXml));
foreach (UrlInfo urlInfo in urlInfos)
{
recentUrlsMenuItem.DropDownItems.Add(new UrlMenuItem(urlInfo));
}
recentUrlsMenuItem.Enabled = true;
}
}
private void SaveRecentUrls()
{
List<UrlInfo> urlInfos = new List<UrlInfo>();
foreach (UrlMenuItem menuItem in recentUrlsMenuItem.DropDownItems)
{
urlInfos.Add(menuItem.UrlInfo);
}
XmlSerializer xs = new XmlSerializer(typeof(List<UrlInfo>));
StringWriter sw = new StringWriter();
xs.Serialize(sw, urlInfos);
Properties.Settings.Default.RecentUrls = sw.ToString();
Properties.Settings.Default.Save();
}
// 在没有这个之前,我使用了:
private void ReadRecentFiles()
{
ConnectionStringsSection section = (ConnectionStringsSection)ConfigurationManager.GetSection(”connectionStrings”);
//I must use <clear/> in <connectionStrings>.
//Or, a defualt sql connection string will be read.
for (int i = 0; i < section.ConnectionStrings.Count; i++)
{
FileInfo fileInfo = new FileInfo(section.ConnectionStrings[i].ConnectionString);
}
private void SaveRecentFiles()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection appSection = (ConnectionStringsSection)config.GetSection(”connectionStrings”);
appSection.ConnectionStrings.Clear();
int count = Math.Min(this.recentFiles.Count, this.recentFilesCount);
for (int i = 0; i < count; i++)
{
FileInfo file = new FileInfo(this.recentFiles[i]);
appSection.ConnectionStrings.Add(new ConnectionStringSettings(file.Name, file.FullName));
}
config.Save();
}