📜  C#中的File.WriteAllBytes()方法与示例

📅  最后修改于: 2021-05-29 16:37:29             🧑  作者: Mango

File.WriteAllBytes(String)是一个内置的File类方法,该方法用于创建一个新文件,然后将指定的字节数组写入该文件,然后关闭该文件。如果目标文件已经存在,则将其覆盖。

句法:

public static void WriteAllBytes (string path, byte[] bytes);

参数:该函数接受两个参数,如下所示:

例外情况:

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

下面是说明File.WriteAllBytes(String,Byte [])方法的程序。
程序1:最初没有创建文件。在代码下面,它自己创建一个文件file.txt并写入一些指定的字节数组,然后最后关闭该文件。

CSharp
// C# program to illustrate the usage
// of File.WriteAllBytes() method
 
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    static void Main(string[] args)
    {
        // Specifying a file name
        var path = @"file.txt";
 
        // Specifying a byte array
        string text = "GFG is a CS portal.";
        byte[] data = Encoding.ASCII.GetBytes(text);
 
        // Calling the WriteAllBytes() function
        // to write specified byte array to the file
        File.WriteAllBytes(path, data);
        Console.WriteLine("The data has been written to the file.");
    }
}


CSharp
// C# program to illustrate the usage
// of File.WriteAllBytes() method
 
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    static void Main(string[] args)
    {
        // Specifying a file name
        var path = @"file.txt";
 
        // Specifying a byte array data
        string text = "GeeksforGeeks";
        byte[] data = Encoding.ASCII.GetBytes(text);
 
        // Calling the WriteAllBytes() function
        // to overwrite the file contents with the
        // specified byte array data
        File.WriteAllBytes(path, data);
        Console.WriteLine("The data has been overwritten to the file.");
    }
}


输出:

The data has been written to the file.

上面的代码提供了如上所示的输出,并创建了一个文件,其中包含一些指定的内容,如下所示:

file.txt

程序2:最初,创建了一个文件,其内容如下所示:

file.txt

下面的代码用指定的字节数组数据覆盖上面的文件内容。

尖锐的

// C# program to illustrate the usage
// of File.WriteAllBytes() method
 
// Using System, System.IO
// and System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    static void Main(string[] args)
    {
        // Specifying a file name
        var path = @"file.txt";
 
        // Specifying a byte array data
        string text = "GeeksforGeeks";
        byte[] data = Encoding.ASCII.GetBytes(text);
 
        // Calling the WriteAllBytes() function
        // to overwrite the file contents with the
        // specified byte array data
        File.WriteAllBytes(path, data);
        Console.WriteLine("The data has been overwritten to the file.");
    }
}

输出:

The data has been overwritten to the file.

运行上述代码后,将显示以上输出,并且文件内容将被覆盖,如下所示:

file.txt