读写XML文件的类及注册表操作

读写系统配置信息在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程序
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Xml.Serialization;
None.gif
using  System.Xml;
None.gif
using  System.Configuration;
None.gif
None.gif
namespace  SendLogMail
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public abstract class SystemConfiguration
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static SystemSettings GetSettings()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            FileStream fs
=null;
InBlock.gif            
string FileName="";
InBlock.gif            SystemSettings data;
InBlock.gif            XmlSerializer serializer 
= new XmlSerializer(typeof(SystemSettings));
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FileName 
= System.Windows.Forms.Application.StartupPath+"\\"+GetSettingsFile();                
InBlock.gif                fs 
= new FileStream(FileName, FileMode.OpenOrCreate);    
InBlock.gif                data 
= (SystemSettings)serializer.Deserialize(fs);                                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Windows.Forms .MessageBox.Show (err.Message
+"系统配置文件出错:\r\n"+FileName);
InBlock.gif                data 
= new SystemSettings();
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (fs!=nulldot.gif{ fs.Close();}                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return data;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void SaveSettings(SystemSettings data)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FileStream fs
=null;
InBlock.gif            
string FileName="";
InBlock.gif            XmlSerializer serializer 
= new XmlSerializer (typeof(SystemSettings));             
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                FileName  
= System.Windows.Forms.Application.StartupPath+"\\"+GetSettingsFile();
InBlock.gif                fs 
= new FileStream(FileName, FileMode.Create);
InBlock.gif                serializer.Serialize(fs, data);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Windows.Forms .MessageBox.Show (err.Message
+"系统配置文件出错:\r\n"+FileName);                
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (fs!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    fs.Close();  
ExpandedSubBlockEnd.gif                }
                
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static string GetSettingsFile()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ConfigurationSettings.AppSettings["SystemSettingsFile"];            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
public class SystemSettings
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
private string _txtSMTPHost;
InBlock.gif        
private string _txtSPSHost;
InBlock.gif                
InBlock.gif        [XmlElement]
InBlock.gif        
public string txtSMTPHost
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _txtSMTPHost; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _txtSMTPHost = value; }
ExpandedSubBlockEnd.gif        }
        
InBlock.gif        [XmlElement]
InBlock.gif        
public string txtSPSHost
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _txtSPSHost; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _txtSPSHost = value; }
ExpandedSubBlockEnd.gif        }

转载于:https://www.cnblogs.com/zyk/archive/2005/03/08/115016.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值