• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • C#處理文本文件

    發表于:2007-05-25來源:作者:點擊數: 標簽:文本文件處理
    文本文件是一種常用的文件格式,所以如何處理文本文件也就成為編程的一個重點。本文就來探討一下用C#是如何來處理文本文件。其內容重點就是如何讀取文本文件內容、如何改變文本文件的內容,以及如何用C#來實現對讀取后的文本文件的打印預覽和打
        文本文件是一種常用的文件格式,所以如何處理文本文件也就成為編程的一個重點。本文就來探討一下用C#是如何來處理文本文件。其內容重點就是如何讀取文本文件內容、如何改變文本文件的內容,以及如何用C#來實現對讀取后的文本文件的打印預覽和打印。  

    一.  本文程序設計和運行的軟件環境:  

    (1).微軟公司視窗2000服務器版  

    (2)..Net  FrameWork  SDK  Beta  2  

    二.  C#處理文本文件的一些重要環節:  

    (1).如何讀取文本文件內容:  

    在本文介紹的程序中,是把讀取的文本文件,用一個richTextBox組件顯示出來。要讀取文本文件,必須使用到"StreamReader"類,這個類是由名字空間"System.IO"中定義的。通過"StreamReader"類的"ReadLine  (  )"方法,就可以讀取打開數據流當前行的數據了。下面代碼實現的功能就是讀取"C:\file.txt"并在richTextBox1組件中顯示出來:  

        FileStream  fs  =  new  FileStream  (  "C:\\file.txt"    ,  FileMode.Open  ,  FileAclearcase/" target="_blank" >ccess.Read  )  ;
            StreamReader  m_streamReader  =  new  StreamReader  (  fs  )  ;  
        //使用StreamReader類來讀取文件
        m_streamReader.BaseStream.Seek  (  0  ,  SeekOrigin.Begin  )  ;
            //  從數據流中讀取每一行,直到文件的最后一行,并在richTextBox1中顯示出內容
            this.richTextBox1.Text  =  ""  ;
            string  strLine  =  m_streamReader.ReadLine  (  )  ;
            while  (  strLine  !=  null  )
            {
                this.richTextBox1.Text  +=  strLine  +  "\n"  ;
                strLine  =  m_streamReader.ReadLine  (  )  ;
            }
            //關閉此StreamReader對象
            m_streamReader.Close  (  )  ;    


    (2).如何改變文本文件中數據內容:  

    在本文介紹的程序中,改變文本文件數據內容的功能是通過改變richTextBox1中的內容來實現的,當richTextBox1這的內容改變后,按動"另存為",就把richTextBox1中內容存儲到指定的文本文件中了。要想改變文本文件內容,要使用到"StreamWriter"類,這個類和"StreamReader"一樣,都是由"System.IO"名字空間來定義的。通過"StreamWriter"類的"Write  (  )"方法,就可以輕松實現文本文件內容的更改了。下面代碼的功能是:如果"C"盤存在"file.txt",則把richTextBox1中的內容寫入到"file.txt"中,如果不存在,則創建此文件,然后在寫入文本數據。  

        //創建一個文件流,用以寫入或者創建一個StreamWriter
        FileStream  fs  =  new  FileStream  (  "C\\file.txt"    ,  FileMode.OpenOrCreate  ,  FileAccess.Write  )  ;
            StreamWriter  m_streamWriter  =  new  StreamWriter  (  fs  )  ;
            m_streamWriter.Flush  (  )  ;
            //  使用StreamWriter來往文件中寫入內容
            m_streamWriter.BaseStream.Seek  (  0  ,  SeekOrigin.Begin  )  ;
            //  把richTextBox1中的內容寫入文件
            m_streamWriter.Write  (  richTextBox1.Text  )  ;
            //關閉此文件
            m_streamWriter.Flush  (  )  ;
            m_streamWriter.Close  (  )  ;    


    從上面這二個代碼可以,寫入數據比起讀取數據要顯得容易些。  

    (3).如何實現打印預覽:  

    打印預覽是通過打印預覽對話框來實現的,實現對讀取得文本文件的打印預覽,最為重要的就是要通知打印預覽對話框所要預覽的文件的內容。下面代碼就是把richTextBox1中顯示的內容,通過打印預覽對話框顯示出來:  

    string  strText  =  richTextBox1.Text  ;
        StringReader  myReader  =  new  StringReader  (  strText  )  ;
        PrintPreviewDialog  printPreviewDialog1  =  new  PrintPreviewDialog  (  )  ;
        printPreviewDialog1.Document  =  ThePrintDocument    ;
        printPreviewDialog1.FormBorderStyle  =  FormBorderStyle.Fixed3D    ;
        printPreviewDialog1.ShowDialog  (  )  ;    


    (4).如何打印文件:  

    在名字空間"System.Drawing.Printing"中定義了一個類"PrintDocument",通過調用此類的"Print"方法就可以觸發在此名字空間中封裝的另外一個事件"PrintPage"。在此事件中設定要打印的文檔內容,從而實現隊文本文件的打印操作。下面代碼是調用"PrintDocument"的"Print"方法,和調用事件"PrintPage"來打印richTextBox1中的內容:  

    ThePrintDocument.Print  (  )  ;//其中ThePrintDocument是"PrintDocument"類的一個對象  

    下列代碼是設定打印內容即打印richTextBox1中的內容:  

                float  linesPerPage  =  0  ;
                    float  yPosition  =  0  ;
                    int  count  =  0  ;
                    float  leftMargin  =  ev.MarginBounds.Left  ;
                    float  topMargin  =  ev.MarginBounds.Top  ;
                    string  line  =  null  ;
                    Font  printFont  =  richTextBox1.Font  ;
                    SolidBrush  myBrush  =  new  SolidBrush  (  Color.Black  )  ;
                    //計算每一頁打印多少行  
                linesPerPage  =  ev.MarginBounds.Height  /  printFont.GetHeight  (  ev.Graphics  )  ;
                    //重復使用StringReader對象  ,打印出richTextBox1中的所有內容
                    while  (  count  <  linesPerPage  &&  (  (  line  =  myReader.ReadLine  (  )  )  !=  null  )  )  
                {
    //  計算出要打印的下一行所基于頁面的位置
        yPosition  =  topMargin  +  (  count  *  printFont.GetHeight  (  ev.Graphics  )  )  ;
        //  打印出richTextBox1中的下一行內容
        ev.Graphics.DrawString  (  line  ,  printFont  ,  myBrush  ,  leftMargin  ,  yPosition  ,  new  StringFormat  (  )  )  ;
        count++  ;
                    }
                    //  判斷如果還要下一頁,則繼續打印
                    if  (  line  !=  null  )
        ev.HasMorePages  =  true  ;
                    else
        ev.HasMorePages  =  false  ;
                    myBrush.Dispose  (  )  ;    


    注釋:由于在上述的代碼中省掉了這些類所對于地名字空間,所以要想成功的編譯和運行上述代碼,就要在程序頭部要導入所使用的名字空間。  

    三.  用C#處理文本文件的完整源程序代碼(control.cs):  

    掌握了上面這些關鍵步驟,就可以方便的得到用C#來處理文本文件的一個完整的源程序,具體如下:  

        using  System  ;
            using  System.Drawing  ;
            using  System.Collections  ;
            using  System.ComponentModel  ;
            using  System.Windows.Forms  ;
            using  System.Data  ;
            using  System.IO  ;
            using  System.Drawing.Printing  ;
        public  class  Form1  :  Form
        {
        private  RichTextBox  richTextBox1  ;
        private  Button  button1  ;
        private  Button  button2  ;
        private  Button  button3  ;
        private  Button  button4  ;
        private  Button  button5  ;
        private  OpenFileDialog  openFileDialog1  ;
        private  SaveFileDialog  saveFileDialog1  ;
        private  PrintDialog  printDialog1  ;
        private  PrintDocument  ThePrintDocument  ;
        private  PrintPreviewDialog  printPreviewDialog1  ;
                            private  StringReader  myReader  ;
        private  System.ComponentModel.Container  components  =  null  ;
        
        public  Form1  (  )
        {
        //初始化窗體中的各個組件
        InitializeComponent  (  )  ;
        }
        //清除程序中使用多的資源
        protected  override  void  Dispose  (  bool  disposing  )
        {
        if  (  disposing  )
        {
        if  (  components  !=  null  )  
    {
    components.Dispose  (  )  ;
        }
        }
        base.Dispose  (  disposing  )  ;
        }
        private  void  InitializeComponent  (  )
        {
        richTextBox1  =  new  RichTextBox  (  )  ;
        button1  =  new  Button  (  )  ;
        button2  =  new  Button  (  )  ;
        button3  =  new  Button  (  )  ;
        button4  =  new  Button  (  )  ;
        button5  =  new  Button  (  )  ;
                        saveFileDialog1  =  new  SaveFileDialog  (  )  ;
                        openFileDialog1  =  new  OpenFileDialog  (  )  ;
        printPreviewDialog1  =  new  PrintPreviewDialog  (  )  ;
        printDialog1  =  new  PrintDialog  (  )  ;
        ThePrintDocument  =  new  PrintDocument  (  )  ;
                                                        ThePrintDocument.PrintPage  +=  new  PrintPageEventHandler  (  ThePrintDocument_PrintPage  )  ;
        SuspendLayout  (  )  ;
        richTextBox1.Anchor  =  AnchorStyles.None  ;
        richTextBox1.Name  =  "richTextBox1"  ;
        richTextBox1.Size  =  new  Size  (  448  ,  280  )  ;
        richTextBox1.TabIndex  =  0  ;
        richTextBox1.Text  =  ""  ;
        button1.Anchor  =  AnchorStyles.None  ;
        button1.Location  =  new  Point  (  41  ,  289  )  ;
        button1.Name  =  "button1"  ;
        button1.Size  =  new  Size  (  48  ,  30  )  ;
        button1.TabIndex  =  1  ;
        button1.Text  =  "打開"  ;
        button1.Click  +=  new  System.EventHandler  (  button1_Click  )  ;
        button2.Anchor  =  AnchorStyles.None  ;
        button2.Location  =  new  Point  (  274  ,  288  )  ;
        button2.Name  =  "button2"  ;
        button2.Size  =  new  Size  (  48  ,  30  )  ;
        button2.TabIndex  =  4  ;
        button2.Text  =  "預覽"  ;
        button2.Click  +=  new  System.EventHandler  (  button2_Click  )  ;
        button3.Anchor  =  AnchorStyles.None  ;
        button3.Location  =  new  Point  (  108  ,  288  )  ;
        button3.Name  =  "button3"  ;
        button3.Size  =  new  Size  (  48  ,  30  )  ;
        button3.TabIndex  =  2  ;
        button3.Text  =  "保存"  ;
        button3.Click  +=  new  System.EventHandler  (  button3_Click  )  ;
        button4.Anchor  =  AnchorStyles.None  ;
        button4.Location  =  new  Point  (  174  ,  288  )  ;
        button4.Name  =  "button4"  ;
        button4.Size  =  new  Size  (  80  ,  30  )  ;
        button4.TabIndex  =  3  ;
        button4.Text  =  "打印機設置"  ;
        button4.Click  +=  new  System.EventHandler  (  button4_Click  )  ;
        button5.Anchor  =  AnchorStyles.None  ;
        button5.Location  =  new  Point  (  345  ,  288  )  ;
        button5.Name  =  "button5"  ;
        button5.Size  =  new  Size  (  48  ,  30  )  ;
        button5.TabIndex  =  5  ;
        button5.Text  =  "打印"  ;
        button5.Click  +=  new  System.EventHandler  (  button5_Click  )  ;
                      saveFileDialog1.DefaultExt  =  "*.txt"  ;
                saveFileDialog1.FileName  =  "file.txt"  ;
        saveFileDialog1.InitialDirectory  =  "c:\\"  ;
        saveFileDialog1.Title  =  "另存為!"  ;
        openFileDialog1.DefaultExt  =  "*.txt"  ;
        openFileDialog1.FileName  =  "file.txt"  ;
        openFileDialog1.InitialDirectory  =  "c:\\"  ;
        openFileDialog1.Title  =  "打開文本文件!"  ;
        AutoScaleBaseSize  =  new  Size  (  6  ,  14  )  ;
        ClientSize  =  new  Size  (  448  ,  325  )  ;
        this.Controls.Add  (  button1  )  ;
        this.Controls.Add  (  button2  )  ;
        this.Controls.Add  (  button3  )  ;
        this.Controls.Add  (  button4  )  ;
        this.Controls.Add  (  button5  )  ;
        this.Controls.Add  (  richTextBox1  )  ;
        
        this.MaximizeBox  =  false  ;
        this.Name  =  "Form1"  ;
        this.Text  =  "C#來操作文本文件"  ;
        this.ResumeLayout  (  false  )  ;
        }
        static  void  Main  (  )  
    {
    Application.Run  (  new  Form1  (  )  )  ;
        }
        
        private  void  button1_Click  (  object  sender  ,  System.EventArgs  e  )
        {
                    try
                    {
        if  (  openFileDialog1.ShowDialog  (  )  ==  DialogResult.OK  )
        {
            FileStream  fs  =  new  FileStream  (  openFileDialog1.FileName    ,  FileMode.Open  ,  FileAccess.Read  )  ;
            StreamReader  m_streamReader  =  new  StreamReader  (  fs  )  ;  
        //使用StreamReader類來讀取文件
        m_streamReader.BaseStream.Seek  (  0  ,  SeekOrigin.Begin  )  ;
            //  從數據流中讀取每一行,直到文件的最后一行,并在richTextBox1中顯示出內容
            this.richTextBox1.Text  =  ""  ;
            string  strLine  =  m_streamReader.ReadLine  (  )  ;
            while  (  strLine  !=  null  )
            {
                this.richTextBox1.Text  +=  strLine  +  "\n"  ;
                strLine  =  m_streamReader.ReadLine  (  )  ;
            }
            //關閉此StreamReader對象
            m_streamReader.Close  (  )  ;
        }  
                }
                catch  (  Exception  em  )
                    {
        Console.WriteLine  (  em.Message.ToString  (  )  )  ;
                    }
        
        }
        
        private  void  button3_Click  (  object  sender  ,  System.EventArgs  e  )
        {
                    try
                    {
        //獲得另存為的文件名稱
        if  (  saveFileDialog1.ShowDialog  (  )  ==  DialogResult.OK  )
        {  
        //創建一個文件流,用以寫入或者創建一個StreamWriter
        FileStream  fs  =  new  FileStream  (  @saveFileDialog1.FileName    ,  FileMode.OpenOrCreate  ,  FileAccess.Write  )  ;
            StreamWriter  m_streamWriter  =  new  StreamWriter  (  fs  )  ;
            m_streamWriter.Flush  (  )  ;
        
            //  使用StreamWriter來往文件中寫入內容
            m_streamWriter.BaseStream.Seek  (  0  ,  SeekOrigin.Begin  )  ;
            //  把richTextBox1中的內容寫入文件
            m_streamWriter.Write  (  richTextBox1.Text  )  ;
            //關閉此文件
            m_streamWriter.Flush  (  )  ;
            m_streamWriter.Close  (  )  ;
        }
                    }
                    catch  (  Exception  em  )
                    {
        Console.WriteLine  (  em.Message.ToString  (  )  )  ;
                    }
        }
        
        private  void  button4_Click  (  object  sender  ,  System.EventArgs  e  )
        {
                    printDialog1.Document  =  ThePrintDocument  ;
                    printDialog1.ShowDialog  (  )  ;
        }
        //預覽打印文檔
        private  void  button2_Click  (  object  sender  ,  System.EventArgs  e  )
        {
                    try
                    {
        string  strText  =  richTextBox1.Text  ;
        myReader  =  new  StringReader  (  strText  )  ;
        PrintPreviewDialog  printPreviewDialog1  =  new  PrintPreviewDialog  (  )  ;
        printPreviewDialog1.Document  =  ThePrintDocument    ;
        printPreviewDialog1.FormBorderStyle  =  FormBorderStyle.Fixed3D    ;
        printPreviewDialog1.ShowDialog  (  )  ;
                    }
                    catch  (  Exception  exp  )
                    {
        Console.WriteLine  (  exp.Message.ToString  (  )  )  ;
                    }
        }
        //打印richTextBox1中的內容
        private  void  button5_Click  (  object  sender  ,  System.EventArgs  e  )
        {
                    printDialog1.Document  =  ThePrintDocument  ;
                    string  strText  =  richTextBox1.Text  ;
                    myReader  =  new  StringReader  (  strText  )  ;
                    if  (  printDialog1.ShowDialog  (  )  ==  DialogResult.OK  )
                    {
        ThePrintDocument.Print  (  )  ;
                    }
        }
                    protected  void  ThePrintDocument_PrintPage  (  object  sender  ,  PrintPageEventArgs  ev  )
                                {
                    float  linesPerPage  =  0  ;
                    float  yPosition  =  0  ;
                    int  count  =  0  ;
                    float  leftMargin  =  ev.MarginBounds.Left  ;
                    float  topMargin  =  ev.MarginBounds.Top  ;
                    string  line  =  null  ;
                    Font  printFont  =  richTextBox1.Font  ;
                    SolidBrush  myBrush  =  new  SolidBrush  (  Color.Black  )  ;
                    //計算每一頁打印多少行  
                linesPerPage  =  ev.MarginBounds.Height  /  printFont.GetHeight  (  ev.Graphics  )  ;
                    //重復使用StringReader對象  ,打印出richTextBox1中的所有內容
                    while  (  count  <  linesPerPage  &&  (  (  line  =  myReader.ReadLine  (  )  )  !=  null  )  )  
                {
    //  計算出要打印的下一行所基于頁面的位置
        yPosition  =  topMargin  +  (  count  *  printFont.GetHeight  (  ev.Graphics  )  )  ;
        //  打印出richTextBox1中的下一行內容
        ev.Graphics.DrawString  (  line  ,  printFont  ,  myBrush  ,  leftMargin  ,  yPosition  ,  new  StringFormat  (  )  )  ;
        count++  ;
                    }
                    //  判斷如果還要下一頁,則繼續打印
                    if  (  line  !=  null  )
        ev.HasMorePages  =  true  ;
                    else
        ev.HasMorePages  =  false  ;
                    myBrush.Dispose  (  )  ;
          }
              }    


    四.  總結:  

    本文雖然只是介紹了用C#處理文本文件,但其實對C#處理其他文件也有很多的參考價值,這是因為在名字空間"System.IO"中定義的用以處理其他文件的類的結構和用以處理文本文件的類的結構有很多是相同的。希望本文對你用C#進行文件方面的編程有所幫助。

    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>