<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# 編程指南) 以下示例給出了將文本寫入文件的各種方法。 前兩個示例對 [System.IO.File](https://msdn.microsoft.com/zh-cn/library/system.io.file.aspx) 類使用靜態便捷方法以將任何 IEnumerable&lt;string&gt; 的每個元素和一個字符串寫入文本文件。 示例 3 展示了在寫入文件時必須分別處理文本的每一行時,如何將文本添加到文件。 示例 1-3 覆蓋文件中的所有現有內容,但示例 4 展示如何將文本追加到現有文件。 這些示例均會將字符串文本寫入文件,但是你更有可能需要使用 [Format](https://msdn.microsoft.com/zh-cn/library/system.string.format.aspx) 方法,此方法具有很多用于寫入不同類型值的控件,包括在字段中左右對齊、有無邊距等。 還可以使用 C# [字符串內插](https://msdn.microsoft.com/zh-cn/library/dn961160.aspx)功能。 ## 示例 <a id="exampleToggle"></a> ``` class WriteTextFile { static void Main() { // These examples assume a "C:\Users\Public\TestFolder" folder on your machine. // You can modify the path if necessary. // Example #1: Write an array of strings to a file. // Create a string array that consists of three lines. string[] lines = { "First line", "Second line", "Third line" }; // WriteAllLines creates a file, writes a collection of strings to the file, // and then closes the file. You do NOT need to call Flush() or Close(). System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines); // Example #2: Write one string to a text file. string text = "A class is the most powerful data type in C#. Like a structure, " + "a class defines the data and behavior of the data type. "; // WriteAllText creates a file, writes the specified string to the file, // and then closes the file. You do NOT need to call Flush() or Close(). System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text); // Example #3: Write only some strings in an array to a file. // The using statement automatically flushes AND CLOSES the stream and calls // IDisposable.Dispose on the stream object. // NOTE: do not use FileStream for text files because it writes bytes, but StreamWriter // encodes the output as text. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt")) { foreach (string line in lines) { // If the line doesn't contain the word 'Second', write the line to the file. if (!line.Contains("Second")) { file.WriteLine(line); } } } // Example #4: Append new text to an existing file. // The using statement automatically flushes AND CLOSES the stream and calls // IDisposable.Dispose on the stream object. using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true)) { file.WriteLine("Fourth line"); } } } //Output (to WriteLines.txt): // First line // Second line // Third line //Output (to WriteText.txt): // A class is the most powerful data type in C#. Like a structure, a class defines the data and behavior of the data type. //Output to WriteLines2.txt after Example #3: // First line // Third line //Output to WriteLines2.txt after Example #4: // First line // Third line // Fourth line ``` 這些示例均會將字符串文本寫入文件,但是你更有可能需要使用 [Format](https://msdn.microsoft.com/zh-cn/library/system.string.format.aspx) 方法,此方法具有很多用于寫入不同類型值的控件,包括在字段中左右對齊、有無邊距等。 還可以使用 C# [字符串內插](https://msdn.microsoft.com/zh-cn/library/dn961160.aspx)功能。 ## 可靠編程 <a id="robustProgrammingToggle"></a> 以下情況可能會導致異常: * 文件已存在并且為只讀。 * 路徑名可能太長。 * 磁盤可能已滿。 ## 請參見 <a id="seeAlsoToggle"></a> #### 概念 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) #### 其他資源 [文件系統和注冊表(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/2kzb96fk.aspx) [示例:將集合保存到應用程序存儲](http://code.msdn.microsoft.com/CSWinStoreAppSaveCollection-bed5d6e6)
                  <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>

                              哎呀哎呀视频在线观看