自己写的一个配置文件读取类

出于工作需要,需要读取本机配置文件,没有现成的配置文件读取类,就自己写了一个,也没有按照什么规范,能用就行。

public   class  ConfigFile
    
{
        
private static string CurrentFile;
        
private static string BaseFile = DBCommon.AppPath + @"\config.bas";
        
private static XmlDocument m_ConfigFile = new XmlDocument();
        
private static XmlNode m_CurrentNode;
        
//        private static ConfigFile()
        
//        {
        ////        }
        //        private static XmlElement m_RootElement;
        public static bool OpenConfigFile( string _file )
        
{
            
try
            
{
                
if!File.Exists( _file ) )
                
{
                    _file 
= DBCommon.DataPath + @"\" + _file;
                    
if!File.Exists( _file ) )
                        File.Copy( BaseFile, _file , 
true );
                }

                CurrentFile 
= _file;
                FileStream fs;
                XmlTextReader xtr;
                fs 
= new FileStream( _file, FileMode.Open, FileAccess.ReadWrite );
                xtr 
= new XmlTextReader(fs);
                m_ConfigFile.Load( xtr );
                m_CurrentNode 
= m_ConfigFile.FirstChild;
                xtr.Close();
                fs.Close();
                
return true;
            }

            
catch( Exception)
            
{
                
return false;
            }

        }


        
public static bool GetRootName( ref string _rootName )
        
{
            
if( m_ConfigFile.Name.Length > 0 )
            
{
                _rootName 
= m_ConfigFile.Name;
                
return true;
            }

            
else
                
return false;
        }

        
public static bool GetValue( string _name, ref string _value )
        
{
            
if( m_CurrentNode.HasChildNodes )
            
{
                
foreach( XmlNode _Node in m_CurrentNode.ChildNodes )
                
{
                    
if( _Node.Name.ToUpper().Equals( _name.ToUpper() ) )
                    
{
                        _value 
= _Node.InnerText;
                        
return true;
                    }

                }

            }

            
return false;
        }

        
public static bool GetValue( string _node, string _name, ref string _value )
        
{
            
try
            
{
                
foreach( XmlNode _Node in m_ConfigFile.ChildNodes )
                
{
                    
if( _Node.Name.ToUpper().Equals( _node.ToUpper() ))
                    
{
                        
foreach( XmlNode _childnode in _Node.ChildNodes )
                        
{
                            
if( _childnode.Name.ToUpper().Equals( _name.ToUpper() ) )
                            
{
                                _value 
=_childnode.InnerText;
                                
return true;
                            }

                        }

                    }

                }

                
return false;
            }

            
catch( Exception)
            
{
                
return false;
            }

        }

        
public static bool SetValue( string _name, string _value )
        
{
            
try
            
{
                
if( m_CurrentNode.HasChildNodes )
                
{
                    
foreach( XmlNode _Node in m_CurrentNode.ChildNodes )
                    
{
                        
if( _Node.Name.ToUpper().Equals( _name.ToUpper() ) )
                        
{
                            _Node.InnerText 
= _value.ToUpper();
                            
return true;
                        }

                    }

                    XmlElement newElement 
= m_ConfigFile.CreateElement( _name );
                    newElement.InnerText 
= _value;
                    m_CurrentNode.AppendChild( newElement );

                }

                
return false;
            }

            
catch( Exception)
            
{
                
return false;
            }

        }

        
public static bool SetValue( string _node, string _name, string _value )
        
{
            
try
            
{
                
foreach( XmlNode _Node in m_ConfigFile.ChildNodes )
                
{
                    
if( _Node.Name.ToUpper().Equals( _node.ToUpper() ))
                    
{
                        
foreach( XmlNode _childnode in _Node.ChildNodes )
                        
{
                            
if( _childnode.Name.ToUpper().Equals( _name.ToUpper() ) )
                            
{
                                _childnode.InnerText 
= _value;
                                
return true;
                            }

                        }

                        XmlElement newElement 
= m_ConfigFile.CreateElement( _name );
                        newElement.InnerText 
= _value;
                        _Node.AppendChild( newElement );
                    }

                }

                
return false;
            }

            
catch( Exception)
            
{
                
return false;
            }

        }

        
public static bool SaveConfigFile( string _file )
        
{
            
try
            
{
                XmlTextWriter xtr;
                xtr 
= new XmlTextWriter( _file, System.Text.Encoding.Unicode );
                m_ConfigFile.WriteTo( xtr );
                
return true;
            }

            
catch( Exception )
            
{
                
return false;
            }

        }

        
public static bool SaveConfigFile()
        
{
            
try
            
{
                
if( File.Exists( CurrentFile ) )
                
{
                    XmlTextWriter xtr;
                    xtr 
= new XmlTextWriter( CurrentFile, System.Text.Encoding.Unicode );
                    m_ConfigFile.WriteTo( xtr );
                    
return true;
                }

                
else
                    
return false;
            }

            
catch( Exception )
            
{
                
return false;
            }

        }

    }

你可能感兴趣的:(配置文件)