日期:2014-05-17  浏览次数:20887 次

C# 将数据写入文件并换行显示
 string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "微博账号.txt");
            if (!File.Exists(path))
                File.Create(path);
            FileInfo finfo = new FileInfo(path);
            if (finfo.Exists)
            {
                finfo.Delete();
            }
            using (FileStream fs = finfo.OpenWrite())
            {
                //根据上面创建的文件流创建写数据流 
                StreamWriter w = new StreamWriter(fs);
                //设置写数据流的起始位置为文件流的末尾 
                w.BaseStream.Seek(0, SeekOrigin.End);
                w.Write("{0}                 {1}\r\n", "微博账号", "昵称");
                foreach (DataGridViewRow dgvr in dgvFansList.SelectedRows)
                {
                    string acccount = dgvr.Cells[2].Value.ToString();
                    string nick = dgvr.Cells[3].Value.ToString();
                    w.Write("{0}          {1}\r\n", acccount, nick);
                }
                //清空缓冲区内容,并把缓冲区内容写入基础流 
                w.Flush();
                //关闭写数据流 
                w.Close();
            }


写入后的格式为: