本文整理汇总了C#中System.IO.File.ReadAllLines方法的典型用法代码示例。如果您正苦于以下问题:C# File.ReadAllLines方法的具体用法?C# File.ReadAllLines怎么用?C# File.ReadAllLines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.File
的用法示例。
在下文中一共展示了File.ReadAllLines方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string[] readText = File.ReadAllLines(path);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string[] createText = { "Hello", "And", "Welcome" };
File.WriteAllLines(path, createText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText, Encoding.UTF8);
// Open the file to read from.
string[] readText = File.ReadAllLines(path, Encoding.UTF8);
foreach (string s in readText)
{
Console.WriteLine(s);
}
}
}
示例3: File.ReadAllLines(String fileName)
//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
class MainClass
{
static void Main(string[] args)
{
StreamWriter writer = new StreamWriter(@"c:\myfile.txt");
for (int i = 0; i < 3; i++)
{
writer.Write(i.ToString());
}
writer.Flush();
writer.Close();
foreach (string line in File.ReadAllLines(@"c:\myfile.txt"))
{
Console.WriteLine(line);
}
}
}