C# 讀取文件

王林
發布: 2024-09-03 15:05:22
原創
551 人瀏覽過

執行檔案操作是程式設計師生活中不可或缺的一部分,所有程式語言都提供各種函式庫或函數來實現相同的目的。在 C# 中,可以使用 File 類別提供者中提供的方法完成相同的操作。一般從檔案讀取是使用ReadAllText(file)和ReadAllLines(file)兩個方法,其中file表示需要讀取的檔案。也可以使用 Streamreader 以位元組形式讀取檔案。本文將詳細介紹 C# 中可用於讀取文件的各種方法以及適當的範例。

文法:

ReadAllText() 具有以下語法

public static string ReadAllText (String Path, System.Text.Encoding encoding)
登入後複製

ReadAllLines() 具有以下語法

public static string ReadAllLines(String, Encoding)
登入後複製

此方法讀取檔案中存在的所有行,然後將它們儲存在字串中,然後關閉檔案。

C#讀取檔案的參數

  • 路徑:路徑包含檔案的位置。必須閱讀此文件。
  • 編碼:表示檔案的編碼類型,可選。

該方法的傳回類型是一個字串,包含文件中的所有內容。此方法在 System.IO 命名空間中可用,與此方法關聯的組件是 mscorlib.dll。

與 ReadAllLines() 方法的 ReadAllText() 相關的異常:

  • 參數異常:當路徑有零個字元、空格或無效字元時會發生這種情況。
  • 參數空異常:當路徑為空時會發生這種情況。
  • 路徑太長異常:當路徑超出實際系統定義的限制時會發生這種情況。
  • IO 異常:開啟檔案時如果發生任何錯誤,就會出現此異常。
  • 未經授權的存取異常:當指定檔案為唯讀時會發生這種情況
  • 找不到檔案異常:當檔案不在指定位置時會發生這種情況。
  • 不支援的例外:當指定的路徑格式無效時,拋出此例外。
  • 安全異常:當使用者無權存取該檔案時,會拋出此異常。

C# 讀取檔案的範例

以下是下面提到的以下範例。

範例#1 – 使用 ReadAllText() 讀取檔案

代碼:

using System; using System.IO; using System.Text; namespace ReadAllText { class Test { static void Main(string[] args) { var Fpath= @"C:\Vignesh\KB.txt"; string content = File.ReadAllText(Fpath, Encoding.UTF8); Console.WriteLine(content); } } }
登入後複製

輸出:

C# 讀取文件

範例 #2 – 使用 ReadAllLines() 讀取檔案

代碼:

using System; using System.IO; using System.Text; namespace ReadAllLines { class Test { static void Main(string[] args) { var inputfile = @"C:\Vignesh\append.txt"; string[] output = File.ReadAllLines(inputfile, Encoding.UTF8); foreach (string op in output) { Console.WriteLine(op); } } } }
登入後複製

輸出:

C# 讀取文件

範例 #3 – 使用 StreamReader 類別讀取檔案

1。 StreamReader.ReadToEnd():該方法用於從目前位置讀取檔案到流末尾。此方法對應的命名空間是System.Io,組件是mscorblib.dll。

文法:

public override string ReadToEnd ();
登入後複製

輸入參數:此方法不需要任何輸入參數。

傳回:此方法將檔案內容作為流輸出,如果當前位置設定為檔案的最後一個字符,則傳回空字串。

2。 StreamReader.ReadLine():此方法從目前流讀取字元並將資料作為字串傳送至輸出。此方法對應的命名空間是System.Io,組件是mscorblib.dll。

文法:

public override string ReadLine();
登入後複製

輸入參數:此方法不需要任何輸入參數。

傳回:傳回目前流的下一行,如果目前流位於最後一行位置則傳回 null。

代碼:

using System; using System.IO; using System.Text; class Program { static void Main(string[] args) { var FP = @"C:\Vignesh\Names.txt"; using var fstre = new FileStream(FP, FileMode.Open, FileAccess.Read); using var sree = new StreamReader(fstre, Encoding.UTF8); string Fcontent = sree.ReadToEnd(); Console.WriteLine(Fcontent); } }
登入後複製

輸出:

C# 讀取文件

代碼:

using System; using System.IO; using System.Text; class Program { static void Main(string[] args) { var filpath = @"C:\Vignesh\TimerJob-2019-08-09.txt"; using var fstre = new FileStream(filpath, FileMode.Open, FileAccess.Read); using var sreee = new StreamReader(fstre, Encoding.UTF8); string cline = String.Empty; while ((cline = sreee.ReadLine()) != null) { Console.WriteLine(cline); } } }
登入後複製

輸出:

C# 讀取文件

代碼:

using System; using System.IO; namespace testclass { class Test { string FPath = @ "C:\Vignesh\Script to 0365 connection.txt"; static void Main(string[] args) { //Check if file is there at the path //ReadallOutput() if (File.Exists(FPath)) { string output = File.ReadAlloutput(FPath); Console.WriteLine(output); } //Check if file is there at the path if (File.Exists(FPath)) { //ReadallLines() string[] Flines = File.ReadAllFlines(FPath); foreach(string line in Flines) Console.WriteLine(line); } //Check if file is there at the path if (File.Exists(FPath)) { //using streamreader using(StreamReader file = new StreamReader(FPath)) { int counter = 0; string lgth; while ((lgth = file.ReadLine()) != null) { Console.WriteLine(lgth); counter++; } file.Close(); } } Console.ReadKey(); } } }
登入後複製

輸出:

C# 讀取文件

範例 #4 – 使用 StreamReader 非同步讀取檔案

代碼:

using System; using System.IO; using System.Text; using System.Threading.Tasks; class TestProgram { static async Task Main(string[] args) { var ip = @" C:\Vignesh\Patching\Patching Steps.txt"; using var fssss = new FileStream(ip, FileMode.Open, FileAccess.Read); using var srrr = new StreamReader(fssss, Encoding.UTF8); //Reading asynchronously string op = await srrr.ReadToEndAsync(); Console.WriteLine(op); } }
登入後複製

輸出:

C# 讀取文件

結論

因此,本文詳細介紹了 C# 中的讀取檔功能。它解釋了可用於執行該操作的各種方法。它還涵蓋了與每種方法相關的各種參數和異常,並結合範例程式的範例進行了詳細解釋。為了更詳細地介紹,建議編寫範例程式並進行練習。

以上是C# 讀取文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!