C#中如何將字符串或者字符串數組寫入文本文件


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//添加引用System.IO

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //字符串數組用System.IO.File.WriteAllLine比較好
            //路徑和文件名、字符串數組名字、最后加上Encoding.Default,防止編碼問題
            string[] a=new string[3]{"111","222","333"};
            File.WriteAllLines("E:\\try.txt",a,Encoding.Default);

            //字符串用System.IO.File.WriteAllText比較好
            //路徑和文件名、字符串名字、最后加上Encoding.Default,防止編碼問題
            string b = "444";
            File.WriteAllText("E:\\try.txt", b, Encoding.Default);

            Console.WriteLine("over");
            Console.ReadKey();
        }
    }
}

 注意:打印的時候會覆蓋,一次性的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM