當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C# File.Create(String)用法及代碼示例

File.Create(String)是一個內置的File類方法,該方法用於覆蓋現有文件,如果指定的文件不存在,則創建一個新文件。

用法:

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

參數:此函數接受如下所示的參數:

  • path: This is the specified file path.

異常:

  • UnauthorizedAccessException:調用者沒有所需的權限。或路徑指定了隻讀文件。或路徑指定了隱藏的文件。
  • ArgumentException:路徑為長度為零的字符串,僅包含空格,或者由InvalidPathChars定義的一個或多個無效字符。
  • ArgumentNullException:路徑為空。
  • PathTooLongException:給定的路徑,文件名或兩者都超過了system-defined的最大長度。
  • DirectoryNotFoundException:給定的路徑無效。
  • IOException:創建文件時發生I /O錯誤。
  • NotSupportedException:給定的路徑格式無效。

下麵是說明File.Create(String)方法的程序。



程序1:最初,沒有文件創建。下麵的代碼本身創建了一個具有指定內容的新文件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() 
    { 
        // Specifing 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()); 
        } 
    } 
}

執行中:

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() 
    { 
        // Specifing 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




相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 File.Create(String) Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。