C#——将数据写入txt

        /// 
        /// 将数据写入txt
        /// 
        /// 
        public void LogWriteTxt(string str)
        {
            string path = Application.StartupPath +"\\"+"result_number.txt";
            str = DateTime.Now.ToString("yyyy-MM-dd") +str;
            if (!File.Exists(path))
            {
                FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);

                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(str);
                sw.Flush();
                sw.Close();
            }
            else
            {
                FileStream fs = new FileStream(path, FileMode.Append);
                //文本写入
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(str);
                sw.Flush();
                sw.Close();
            }
        }

 

你可能感兴趣的:(原创,c#)