最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年02月01日 17:21:09   投稿:goldensun  
這篇文章主要介紹了Windows系統(tǒng)中使用C#讀取文本文件內(nèi)容的小示例,包括一次一行地讀取文本文件的方法,需要的朋友可以參考下

讀取文本文件中的內(nèi)容

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

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 方法將文本文件的內(nèi)容讀?。ㄒ淮巫x取一行)到字符串中。所有文本行都保存在字符串 line 中并顯示在屏幕上。

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();

相關(guān)文章

最新評論

广宁县| 太湖县| 乌恰县| 茂名市| 寿光市| 赤壁市| 高尔夫| 西青区| 淮滨县| 东乌珠穆沁旗| 同心县| 永新县| 远安县| 富宁县| 海丰县| 江油市| 普洱| 白水县| 广汉市| 石渠县| 都匀市| 衡山县| 禄丰县| 雅江县| 安多县| 鹤山市| 孝昌县| 唐山市| 苍梧县| 攀枝花市| 泉州市| 浦城县| 福清市| 太原市| 鄯善县| 鱼台县| 葫芦岛市| 桐梓县| 泾源县| 原平市| 逊克县|