读写系统配置信息在win32下常用注册表,在.net下可以沿用这种方法
using Microsoft.Win32;
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN",true);
regKey.SetValue ("AutoSendLogMail", ExefileName);
regKey.DeleteValue ("AutoSendLogMail",false);
但更好的方法是写到XML文件中,优点是修改和移植方便,并且可以将元素和类的属性进行映射,序列化与反序列化;
于是参考.Text中相关的类写了一个读写XML文件的类
用于WinForm程序
using
System;
using
System.IO;
using
System.Xml.Serialization;
using
System.Xml;
using
System.Configuration;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
SendLogMail
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public abstract class SystemConfiguration
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static SystemSettings GetSettings()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream fs=null;
string FileName="";
SystemSettings data;
XmlSerializer serializer = new XmlSerializer(typeof(SystemSettings));
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileName = System.Windows.Forms.Application.StartupPath+"\\"+GetSettingsFile();
fs = new FileStream(FileName, FileMode.OpenOrCreate);
data = (SystemSettings)serializer.Deserialize(fs);
}
catch (Exception err)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Windows.Forms .MessageBox.Show (err.Message+"系统配置文件出错:\r\n"+FileName);
data = new SystemSettings();
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (fs!=null)
{ fs.Close();}
}
return data;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public static void SaveSettings(SystemSettings data)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream fs=null;
string FileName="";
XmlSerializer serializer = new XmlSerializer (typeof(SystemSettings));
try
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileName = System.Windows.Forms.Application.StartupPath+"\\"+GetSettingsFile();
fs = new FileStream(FileName, FileMode.Create);
serializer.Serialize(fs, data);
}
catch (Exception err)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Windows.Forms .MessageBox.Show (err.Message+"系统配置文件出错:\r\n"+FileName);
}
finally
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (fs!=null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fs.Close();
}
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private static string GetSettingsFile()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ConfigurationSettings.AppSettings["SystemSettingsFile"];
}
}
public class SystemSettings
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string _txtSMTPHost;
private string _txtSPSHost;
[XmlElement]
public string txtSMTPHost
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _txtSMTPHost; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _txtSMTPHost = value; }
}
[XmlElement]
public string txtSPSHost
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _txtSPSHost; }
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _txtSPSHost = value; }
}