File.WriteAllLines Method (String, IEnumerable<String>)
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Creates a new file, writes a collection of strings to the file, and then closes the file.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Parameters
- path
- Type: System.String
The file to write to.
- contents
- Type: System.Collections.Generic.IEnumerable<String>
The lines to write to the file.
| Exception | Condition |
|---|---|
| ArgumentException |
path is a zero-length string, contains only white space, or contains one or more invalid characters defined by the GetInvalidPathChars method. |
| ArgumentNullException |
Either path or contents is null. |
| DirectoryNotFoundException |
path is invalid (for example, it is on an unmapped drive). |
| FileNotFoundException |
The file specified by path was not found. |
| IOException |
An I/O error occurred while opening the file. |
| PathTooLongException |
path exceeds the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters. |
| NotSupportedException |
path is in an invalid format. |
| SecurityException |
The caller does not have the required permission. |
| UnauthorizedAccessException |
path specifies a file that is read-only. -or- This operation is not supported on the current platform. -or- path is a directory. -or- The caller does not have the required permission. |
If the target file already exists, it is overwritten.
You can use this method to create the contents for a collection class that takes an IEnumerable<T> in its constructor, such as a List<T>, HashSet<T>, or a SortedSet<T> class.
The following example writes selected lines from a sample data file to a file.
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()); } } } }
Windows 8 Consumer Preview, Windows Server 8 Beta, Windows 7, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.