Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.
public:
static void WriteAllLines(System::String ^ path, cli::array <System::String ^> ^ contents, System::Text::Encoding ^ encoding);
public static void WriteAllLines(string path, string[] contents, System.Text.Encoding encoding);
static member WriteAllLines : string * string[] * System.Text.Encoding -> unit
Public Shared Sub WriteAllLines (path As String, contents As String(), encoding As Encoding)
Parameters
The file to write to.
The string array to write to the file.
An Encoding object that represents the character encoding applied to the string array.
Exceptions.NET Framework and .NET Core versions older than 2.1: path
is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.
Either path
or contents
is null
.
The specified path, file name, or both exceed the system-defined maximum length.
The specified path is invalid (for example, it is on an unmapped drive).
An I/O error occurred while opening the file.
path
specified a file that is read-only.
-or-
path
specified a file that is hidden.
-or-
This operation is not supported on the current platform.
-or-
path
specified a directory.
-or-
The caller does not have the required permission.
path
is in an invalid format.
The caller does not have the required permission.
ExamplesThe following code example demonstrates the use of the WriteAllLines method to write text to a file. In this example a file is created, if it doesn't already exist, and text is added to it.
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);
}
}
}
open System
open System.IO
open System.Text
let path = @"c:\temp\MyTest.txt"
// This text is added only once to the file.
if File.Exists path |> not then
// Create a file to write to.
let 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.
let appendText =
"This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
// Open the file to read from.
let readText = File.ReadAllLines(path, Encoding.UTF8)
for s in readText do
printfn $"{s}"
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText, Encoding.UTF8)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText, Encoding.UTF8)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path, Encoding.UTF8)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
Remarks
If the target file already exists, it is truncated and overwritten.
Given a string array and a file path, this method opens the specified file, writes the string array to the file using the specified encoding, and then closes the file.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4