一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - Windows系統(tǒng)中使用C#讀取文本文件內容的小示例

Windows系統(tǒng)中使用C#讀取文本文件內容的小示例

2021-11-11 14:40C#教程網(wǎng) C#

這篇文章主要介紹了Windows系統(tǒng)中使用C#讀取文本文件內容的小示例,包括一次一行地讀取文本文件的方法,需要的朋友可以參考下

讀取文本文件中的內容

此示例讀取文本文件的內容以使用 System.IO.File 選件類的靜態(tài)方法 ReadAllText 和 ReadAllLines。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class ReadFromFile
{
  static void Main()
  {
    // The files used in this example are created in the topic
    // How to: Write to a Text File. You can change the path and
    // file name to substitute text files of your own.
 
    // Example #1
    // Read the file as one string.
    string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
 
    // Display the file contents to the console. Variable text is a string.
    System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
 
    // Example #2
    // Read each line of the file into a string array. Each element
    // of the array is one line of the file.
    string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
 
    // Display the file contents by using a foreach loop.
    System.Console.WriteLine("Contents of WriteLines2.txt = ");
    foreach (string line in lines)
    {
      // Use a tab to indent each line of the file.
      Console.WriteLine("\t" + line);
    }
 
    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    System.Console.ReadKey();
  }
}

一次一行地讀取文本文件
本示例使用 StreamReader 類的 ReadLine 方法將文本文件的內容讀取(一次讀取一行)到字符串中。所有文本行都保存在字符串 line 中并顯示在屏幕上。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int counter = 0;
string line;
 
// Read the file and display it line by line.
System.IO.StreamReader file =
  new System.IO.StreamReader(@"c:\test.txt");
while((line = file.ReadLine()) != null)
{
  System.Console.WriteLine (line);
  counter++;
}
 
file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女鸡 | 欧洲男同直粗无套播放视频 | 亚洲AV无码国产精品午夜久久 | 欧美人shou交在线播放 | 国产精品美女久久久久 | 欧亚精品一区二区三区 | 亚洲一区二区福利视频 | 久久精品国产欧美日韩99热 | pregnantsexxx临盆孕妇 | 色多多视频在线 | 成人免费片| 初尝黑人巨大h文 | 欧美同性gayvidoes | bl文全肉高h湿被灌尿 | 精品91自产拍在线观看99re | 国产成人福利美女观看视频 | 美女张开下身让男人桶 | 日本粉色视频 | 1769在线观看 | 猫咪社区在线播放 | 色狠狠狠狠狠香蕉 | 扒开女人下面使劲桶屁股动漫 | 热久久免费| 午夜亚洲一区二区福利 | 毛片在线网址 | 不良小说| 亚洲AV永久无码精品澳门 | 丝袜足控免费网站xx动漫漫画 | 国内免费高清视频在线观看 | 日本人黄色 | 日韩高清一区二区 | 精品国产一区二区三区在线观看 | 91视频一区| 无人在线视频高清免费观看动漫 | 日本免费三片在线观看 | 狠狠色狠狠色综合婷婷tag | 1313午夜精品理伦片 | 国产免费一区二区三区免费视频 | 亚洲国产精品网站久久 | 国产精品原创永久在线观看 | 久久这里有精品 |