写入Txt文本信息

 public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> lists = new List<string>();
            lists.Add("您好");
            lists.Add("测试1");
            lists.Add("测试2");
            lists.Add("测试3");

            WriteTXT("D:/Test.txt", lists);
            MessageBox.Show("写入成功!");
        }

        public void WriteTXT(string path, List<string> datas)
        {
            try
            {
                using (FileStream fs = File.OpenWrite(path))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        foreach (var data in datas)
                        {
                            sw.Write(data + "\r\n");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //WinTools.ShowTipMsgBox(ex.Message);
                //LogLib.MyLog.SetErrorLog(ex);
            }
        }

猜你喜欢

转载自www.cnblogs.com/yuanshuo/p/11733707.html