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


C# File.WriteAllLines方法代码示例

本文整理汇总了C#中System.IO.File.WriteAllLines方法的典型用法代码示例。如果您正苦于以下问题:C# File.WriteAllLines方法的具体用法?C# File.WriteAllLines怎么用?C# File.WriteAllLines使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.File的用法示例。


在下文中一共展示了File.WriteAllLines方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: 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.WriteAllLines

示例2: 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.WriteAllLines

示例3: Main

//引入命名空间
using System;
using System.IO;
using System.Linq;

class Program
{
    static string dataPath = @"c:\temp\timestamps.txt";

    static void Main(string[] args)
    {
        CreateSampleFile();

        var JulyWeekends = from line in File.ReadLines(dataPath)
                           where (line.StartsWith("Saturday") || 
                           line.StartsWith("Sunday")) & 
                           line.Contains("July")
                           select line;

        File.WriteAllLines(@"C:\temp\selectedDays.txt", JulyWeekends);

        var MarchMondays = from line in File.ReadLines(dataPath)
                           where line.StartsWith("Monday") && 
                           line.Contains("March")
                           select line;

        File.AppendAllLines(@"C:\temp\selectedDays.txt", MarchMondays);
    }

    static void CreateSampleFile()
    {
        DateTime TimeStamp = new DateTime(1700, 1, 1);

        using (StreamWriter sw = new StreamWriter(dataPath))
        {
            for (int i = 0; i < 500; i++)
            {
                DateTime TS1 = TimeStamp.AddYears(i);
                DateTime TS2 = TS1.AddMonths(i);
                DateTime TS3 = TS2.AddDays(i);
                sw.WriteLine(TS3.ToLongDateString());
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:45,代码来源:File.WriteAllLines

示例4: File.WriteAllLines(String fileName, String[] lines)

//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class MainClass
{
    static void Main(string[] args)
    {
        string[] lines = new string[10];
        for (int i = 0; i < 10; i++)
        {
            lines[i] = String.Format( "This is line number {0}", i);
        }

        File.WriteAllLines(@"c:\test.txt", lines);

        foreach (string line in File.ReadAllLines(@"c:\test.txt"))
        {
            Console.WriteLine(line);
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:24,代码来源:File.WriteAllLines


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