📌  相关文章
📜  C#中带有示例的File.WriteAllLines(String,String [],Encoding)方法

📅  最后修改于: 2021-05-29 17:49:28             🧑  作者: Mango

File.WriteAllLines(String,String [],Encoding)是一个内置的File类方法,用于创建新文件,使用指定的编码将指定的字符串数组写入文件,然后关闭文件。
句法:

参数:此函数接受三个参数,如下所示:

例外情况:

  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径内容为null。
  • PathTooLongException:指定的路径,文件名或两者都超过系统定义的最大长度。
  • DirectoryNotFoundException:指定的路径无效。
  • IOException:打开文件时发生I / O错误。
  • UnauthorizedAccessException:路径指定了一个只读文件。或路径指定了隐藏的文件。或当前平台不支持此操作。或路径指定目录。或呼叫者没有所需的权限。
  • NotSupportedException:路径格式无效。
  • SecurityException:调用者没有所需的权限。

下面是说明File.WriteAllLines(String,String [])方法的程序。
程序1:最初没有创建文件。下面的代码本身创建一个文件file.txt并将指定的字符串数组写入该文件。

C#
// C# program to illustrate the usage
// of File.WriteAllLines(String, String[], 
// Encoding) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
  
        // Creating some string array to
        // write into the file
        string[] createText = { "GFG", "is a", "CS portal." };
  
        // Calling WriteAllLines() function to write
        // the specified string array into the file
        File.WriteAllLines(path, createText, Encoding.UTF8);
  
        // Reading the file contents
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach(string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}


C#
// C# program to illustrate the usage
// of File.WriteAllLines(String, 
// String[], Encoding) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
  
        // Creating some string array to
        // write into the file
        string[] createText = { "GFG", "Geeks", "GeeksforGeeks" };
  
        // Calling WriteAllLines() function to overwrite
        // the specified string array into the file
        File.WriteAllLines(path, createText, Encoding.UTF8);
  
        // Reading the file contents
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach(string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}


输出:

GFG
is a
CS portal.

运行上述代码后,将显示以上输出,并创建一个新文件file.txt,如下所示-

file.txt

程序2:最初,将创建一个文件file.txt ,其内容如下所示-

file.txt

下面的代码使用指定的字符串数组覆盖文件内容。

C#

// C# program to illustrate the usage
// of File.WriteAllLines(String, 
// String[], Encoding) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifying a file
        string path = @"file.txt";
  
        // Creating some string array to
        // write into the file
        string[] createText = { "GFG", "Geeks", "GeeksforGeeks" };
  
        // Calling WriteAllLines() function to overwrite
        // the specified string array into the file
        File.WriteAllLines(path, createText, Encoding.UTF8);
  
        // Reading the file contents
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach(string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

输出:

GFG
Geeks
GeeksforGeeks

运行上面的代码后,将显示上面的输出,文件file.txt的内容如下所示-

file.txt