📌  相关文章
📜  C#中的File.Create(String)方法与示例

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

File.Create(String)是一个内置的File类方法,该方法用于覆盖现有文件,如果指定的文件不存在,则创建一个新文件。
句法:

public static System.IO.FileStream Create (string path);

参数:该函数接受如下所示的参数:

例外情况:

  • UnauthorizedAccessException:调用者没有所需的权限。或路径指定了只读文件。或路径指定了隐藏的文件。
  • ArgumentException:路径是长度为零的字符串,仅包含空格,或者由InvalidPathChars定义的一个或多个无效字符。
  • ArgumentNullException:路径为null。
  • PathTooLongException:给定的路径,文件名或两者都超过了系统定义的最大长度。
  • DirectoryNotFoundException:给定的路径无效。
  • IOException:创建文件时发生I / O错误。
  • NotSupportedException:给定的路径格式无效。

下面是说明File.Create(String)方法的程序。
程序1:最初没有创建文件。下面的代码本身创建了一个具有指定内容的新文件file.txt

CSharp
// C# program to illustrate the usage
// of File.Create(String) 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 path
        string file_path = @"file.txt";
 
        try {
            // Creating a new file, or overwrite
            // if the file already exists.
            using(FileStream fs = File.Create(file_path))
            {
                // Adding some info into the file
                byte[] info = new UTF8Encoding(true).GetBytes("GeeksforGeeks");
                fs.Write(info, 0, info.Length);
            }
 
            // Reading the file contents
            using(StreamReader sr = File.OpenText(file_path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) {
                    Console.WriteLine(s);
                }
            }
        }
 
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}


CSharp
// C# program to illustrate the usage
// of File.Create(String) 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 path
        string file_path = @"file.txt";
 
        try {
            // Overwriting the above file contents
            using(FileStream fs = File.Create(file_path))
            {
                // Adding some info into the file
                byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS Portal");
                fs.Write(info, 0, info.Length);
            }
 
            // Reading the file contents
            using(StreamReader sr = File.OpenText(file_path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) {
                    Console.WriteLine(s);
                }
            }
        }
 
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}


执行中:

mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks

运行以上代码后,将显示以上输出,并创建一个新文件file.txt ,其中包含一些指定的内容,如下所示:

file.txt

程序2:在运行以下代码之前,将创建以下所示的文件file.txt

file.txt

尖锐的

// C# program to illustrate the usage
// of File.Create(String) 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 path
        string file_path = @"file.txt";
 
        try {
            // Overwriting the above file contents
            using(FileStream fs = File.Create(file_path))
            {
                // Adding some info into the file
                byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS Portal");
                fs.Write(info, 0, info.Length);
            }
 
            // Reading the file contents
            using(StreamReader sr = File.OpenText(file_path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null) {
                    Console.WriteLine(s);
                }
            }
        }
 
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
    }
}

执行中:

mcs -out:main.exe main.cs
mono main.exe
GFG is a CS Portal

运行以上代码后,将显示以上输出,并且现有文件内容将被覆盖。

file.txt