Outstanding's Blog

outstanding

类似Delphi中的Strings类型

熟悉Delphi的人应该对Strings不陌生:

using System;
using System.Collections;
using System.IO;

namespace TextFileOP
{
    
/**//// <summary>
    
/// Strings 的摘要说明。
    
/// </summary>

    public class Strings:CollectionBase
    
{
        
public String thisint index ]    
        
{
            
get    
            
{
                
return( (string) List[index] );
            }

            
set    
            
{
                List[index] 
= value;
            }

        }

        
public int Add( string value )    
        
{
            
return( List.Add( value ) );
        }


        
public int IndexOf( string value )    
        
{
            
return( List.IndexOf( value ) );
        }


        
public void Insert( int index, string value )    
        
{
            List.Insert( index, value );
        }


        
public void Remove( string value )    
        
{
            List.Remove( value );
        }


        
public bool Contains( string value )    
        
{
            
return( List.Contains( value ) );
        }


        
protected override void OnInsert( int index, Object value )    
        
{
            
if ( value.GetType() != Type.GetType("System.String") )
                
throw new ArgumentException( "value 参数应为 string 类型.""value" );
        }


        
protected override void OnRemove( int index, Object value )    
        
{
            
if ( value.GetType() != Type.GetType("System.String") )
                
throw new ArgumentException( "value 参数应为 string 类型.""value" );
        }


        
protected override void OnSet( int index, Object oldValue, Object newValue )    
        
{
            
if ( newValue.GetType() != Type.GetType("System.String") )
                
throw new ArgumentException( "value 参数应为 string 类型.""newValue" );
        }


        
protected override void OnValidate( Object value )    
        
{
            
if ( value.GetType() != Type.GetType("System.String") )
                
throw new ArgumentException( "value 参数应为 string 类型." );
        }


        
public void LoadFromFile(string filename)
        
{
            
try 
            
{
                
using (StreamReader sr = new StreamReader(filename)) 
                
{
                    String line;
                    
while ((line = sr.ReadLine()) != null
                    
{
                        
this.Add(line);
                    }

                }

            }

            
catch (Exception e) 
            
{
                
throw(e);
            }


        }


        
public void SaveToFile(string filename,bool overwrite)
        
{
            
if (File.Exists(filename) && (!overwrite))
            
{
                
throw(new Exception("文件已存在!"));
            }

            StreamWriter sr 
=  null;
            
try
            
{
                sr 
= File.CreateText(filename);
                
for(int i=0;i<this.Count;i++)
                 sr.WriteLine(
this[i]);
            }

            
catch(Exception e)
            
{
                
throw(e);
            }

            
finally
            
{
                sr.Close();
            }

        }

    }

}

posted on 2005-01-24 13:23  Outstanding  阅读(843)  评论(1)    收藏  举报

导航