<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 如何:創建文件或文件夾(C# 編程指南) 您可通過編程方式在您的計算機上創建文件夾、子文件夾和子文件夾中的文件,并將數據寫入文件。 ``` public class CreateFileOrFolder { static void Main() { // Specify a name for your top-level folder. string folderName = @"c:\Top-Level Folder"; // To create a string that specifies the path to a subfolder under your // top-level folder, add a name for the subfolder to folderName. string pathString = System.IO.Path.Combine(folderName, "SubFolder"); // You can write out the path name directly instead of using the Combine // method. Combine just makes the process easier. string pathString2 = @"c:\Top-Level Folder\SubFolder2"; // You can extend the depth of your path if you want to. //pathString = System.IO.Path.Combine(pathString, "SubSubFolder"); // Create the subfolder. You can verify in File Explorer that you have this // structure in the C: drive. // Local Disk (C:) // Top-Level Folder // SubFolder System.IO.Directory.CreateDirectory(pathString); // Create a file name for the file you want to create. string fileName = System.IO.Path.GetRandomFileName(); // This example uses a random string for the name, but you also can specify // a particular name. //string fileName = "MyNewFile.txt"; // Use Combine again to add the file name to the path. pathString = System.IO.Path.Combine(pathString, fileName); // Verify the path that you have constructed. Console.WriteLine("Path to my file: {0}\n", pathString); // Check that the file doesn't already exist. If it doesn't exist, create // the file and write integers 0 - 99 to it. // DANGER: System.IO.File.Create will overwrite the file if it already exists. // This could happen even with random file names, although it is unlikely. if (!System.IO.File.Exists(pathString)) { using (System.IO.FileStream fs = System.IO.File.Create(pathString)) { for (byte i = 0; i < 100; i++) { fs.WriteByte(i); } } } else { Console.WriteLine("File \"{0}\" already exists.", fileName); return; } // Read and display the data from your file. try { byte[] readBuffer = System.IO.File.ReadAllBytes(pathString); foreach (byte b in readBuffer) { Console.Write(b + " "); } Console.WriteLine(); } catch (System.IO.IOException e) { Console.WriteLine(e.Message); } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } // Sample output: // Path to my file: c:\Top-Level Folder\SubFolder\ttxvauxe.vv0 //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 //30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 // 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8 //3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 } ``` 如果該文件夾已存在,則 [CreateDirectory](https://msdn.microsoft.com/zh-cn/library/system.io.directory.createdirectory.aspx) 不執行任何操作,且不會引發異常。但是,[File.Create](https://msdn.microsoft.com/zh-cn/library/system.io.file.create.aspx) 用新的文件替換現有文件。該示例使用一個 **if**-**else** 語句阻止現有文件被替換。 通過在示例中做出以下更改,您可以根據具有某個名稱的程序是否存在來指定不同的結果。如果該文件不存在,代碼將創建一個文件。如果該文件存在,代碼將把數據添加到該文件中。 * 指定一個非隨機文件名。 ``` // Comment out the following line. //string fileName = System.IO.Path.GetRandomFileName(); // Replace that line with the following assignment. string fileName = "MyNewFile.txt"; ``` * 用以下代碼中的 **using** 語句替換 **if**-**else** 語句。 ``` using (System.IO.FileStream fs = new System.IO.FileStream(pathString, FileMode.Append)) { for (byte i = 0; i &lt; 100; i++) { fs.WriteByte(i); } } ``` 運行該示例若干次以驗證數據是否每次都添加到文件中。 有關更多可以嘗試的 **FileMode** 值的信息,請參閱 [FileMode](https://msdn.microsoft.com/zh-cn/library/system.io.filemode.aspx)。 以下情況可能會導致異常: * 文件夾名稱格式不正確。例如,它包含非法字符或僅僅是空白([ArgumentException](https://msdn.microsoft.com/zh-cn/library/system.argumentexception.aspx) 類)。使用 [Path](https://msdn.microsoft.com/zh-cn/library/system.io.path.aspx) 類創建有效路徑名。 * 要創建的文件夾的父文件夾是只讀的([IOException](https://msdn.microsoft.com/zh-cn/library/system.io.ioexception.aspx) 類)。 * 文件夾名稱是 **null**([ArgumentNullException](https://msdn.microsoft.com/zh-cn/library/system.argumentnullexception.aspx) 類)。 * 文件夾名稱太長([PathTooLongException](https://msdn.microsoft.com/zh-cn/library/system.io.pathtoolongexception.aspx) 類)。 * 文件夾名稱只是冒號“:”([PathTooLongException](https://msdn.microsoft.com/zh-cn/library/system.io.pathtoolongexception.aspx) 類)。 ## .NET Framework 安全性 在部分信任的情況下可能會引發 [SecurityException](https://msdn.microsoft.com/zh-cn/library/system.security.securityexception.aspx) 類的實例。 如果沒有創建文件夾的權限,則該示例引發 [UnauthorizedAccessException](https://msdn.microsoft.com/zh-cn/library/system.unauthorizedaccessexception.aspx) 類的實例。 ## 請參閱 [System.IO](https://msdn.microsoft.com/zh-cn/library/system.io.aspx) [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [文件系統和注冊表(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/2kzb96fk.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看