加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#在桌面上保存txt文件

发布时间:2020-12-15 04:02:45 所属栏目:百科 来源:网络整理
导读:如何保存我在桌面上创建的txt文件? 这是代码: void CreaTxtBtnClick(object sender,EventArgs e){ string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); filePath = filePath + @"Error Log"; TextWriter sw = new StreamW
如何保存我在桌面上创建的txt文件?

这是代码:

void CreaTxtBtnClick(object sender,EventArgs e){
    string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    filePath = filePath + @"Error Log";
    TextWriter sw = new StreamWriter(@"Gara.txt");

    int rowcount = dataGridView1.Rows.Count;
    for(int i = 0; i < rowcount - 1; i++){
        sw.WriteLine(
            dataGridView1.Rows[i].Cells[0].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[1].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[2].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[3].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[4].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[5].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[6].Value.ToString() + 't' +
            dataGridView1.Rows[i].Cells[7].Value.ToString() + 't'
        );
    }
    sw.Close();
    MessageBox.Show("File txt creato correttamente");
}

我按照这些指示思考

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
filePath = filePath + @"Error Log";
TextWriter sw = new StreamWriter(@"Gara.txt");

我可以将文件保存在桌面上,但是在错误的路径中正确创建了txt.
我该如何解决?

解决方法

您已构建了filePath,但尚未在TextWriter中使用它.相反,您只需要写入Gara.txt文件,该文件默认位于应用程序启动的文件夹中.

将您的代码更改为:

filePath = filePath +@"Error LogGara.txt";
 TextWriter sw= new StreamWriter(filePath);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读