当前位置: 首页>>代码示例>>C#>>正文


C# File.ReadAllLines方法代码示例

本文整理汇总了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);
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:30,代码来源:File.ReadAllLines

示例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);
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:32,代码来源:File.ReadAllLines

示例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);
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:24,代码来源:File.ReadAllLines


注:本文中的System.IO.File.ReadAllLines方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。