本文整理汇总了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);
}
}
}
示例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);
}
}
}
示例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());
}
}
}
}
示例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);
}
}
}