.NET學習筆記:檔案和註冊表操作

來源:互聯網
上載者:User
  • 資料流:所有流式類的(抽象)基類都是System.IO命名空間下的Stream類。Stream類允許通過檔案、網路、通訊端流動資料。我們可以使加密的資料流動,也可以使資料流緩衝處理。
  • 通過緩衝流動資料的優點是:資料流的來源和目的地沒有必要在整個資料流動過程中都保持串連:只是在每次緩衝區被沖刷的時候,兩者的串連是可用的就可以了。保持串連是消耗資源的行為,所以緩衝可以改善系統的效能。
  • 可供使用的Stream類:
    FileStream:用來向檔案讀寫資料,它還提供了開啟、尋找、關閉檔案的功能
    NetworkStream:它允許你訪問TCP/IP通訊端(在System.Net.Socket命名空間)
    MemoryStream:允許用流的方法在記憶體中建立一個可訪問的可擴充的地區,也允許把預分配的記憶體地區當作流看待
    BufferedStream:公用程式類,可以向一個未緩衝的流增加緩衝
    CryptoStream:公用程式類,位於System.Security.Cryptography命名空間,可以加密或解密傳遞的資訊
  • FileStream類的構造參數
    string path 要訪問的檔案名稱和路徑
    FileMode mode 指定檔案的開啟檔案,或建立檔案
    FileAccess access 檔案的讀寫權限,如Read(讀)、Write(寫)、ReadWrite(讀寫 預設)
    FileShare share 其它進程對正在使用檔案的存取權限,如None、Read(預設)、Write、ReadWrite
    int bufferSize 緩衝區大小,通常保持預設的4096 bytes
  • FileMode開啟檔案枚舉
    Append 開啟已存在的檔案並尋到結尾,或建立一檔案
    Create 建立一檔案,如果檔案已存在,則覆蓋
    CreateNew 建立一檔案,如果已存在,則拋出IOException
    Open 開啟,如果不存在,拋出FileNotFoundException
    OpenOrCreate 開啟,如果不存在則建立
    Truncate 開啟,並將長度置為0,如果不存在,拋出FileNotFoundException
  • 例:
    FileStream fs = new FileStream( @“C:\temp\Demo.dat“, FileMode.Create, FileAccess.Write );
  • 為了傳輸文本,建立一個Reader或Writer對象,並把它們附在流上,並在它們中使用方法,如WriteLine()方法。因此可以把Reader和Writer類看做是流和代碼之間起互動作用的公用程式類:StreamReader、StreamWriter和BinaryReader、BinaryWriter
  • StreamWriter類使用UTF-8做為預設的編碼方案。
  • StreamReader的建構函式參數:
    Stream stream 一個開放的流,如FileStream
    Encoding encoding 編碼方案,一般可以使用Encoding.Default
    int bufferSize 緩衝區大小(使用預設即可)
    string path 不指定Stream而指定檔案,StreamReader將用後台方式開啟一個流並用它從檔案讀取資料
    bool detect 是否自動檢測檔案中的編碼格式(預設為true)
  • StreamReader的方法:
    Read() 在文字資料流中讀文本符號
    ReadLine() 在流中讀一行並以字串變數的形式返回
    ReadToEnd() 從當前位置到結尾讀入一個獨立的字串
    Close() 關閉StreamReader及其下屬的流
  • 在OpenFileDialog中,如果需要過濾2種以上的格式,可以在副檔名之間加上分號即可。如:文字檔(*.txt *.rtf)|*.txt;*.rtf|全部檔案(*.*)|*.*
  • StringCollection類似一個可擴充的字串數組,因為事先無法確定數組的數量(文本的行數)
  • 例:
    public void ReadFile()
    {
    if( opdMain.ShowDialog() == DialogResult.OK )
    {
    FileStream fs = new FileStream( opdMain.FileName, FileMode.Open, FileAccess.ReadWrite );
    StreamReader aStreamReader = new StreamReader( fs, Encoding.Default );
    StringCollection stringCollection = new StringCollection();
    string onLine;
    while( ( onLine = aStreamReader.ReadLine() ) != null )
    {
    stringCollection.Add( onLine );
    }
    aStreamReader.Close();
    string[] stringArray = new string[ stringCollection.Count ];
    stringCollection.CopyTo( stringArray, 0 );
    rtfEditor.Lines = stringArray;
    }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.