微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#在桌面上保存txt文件

如何保存我在桌面上创建的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 Log\Gara.txt";
 TextWriter sw= new StreamWriter(filePath);

原文地址:https://www.jb51.cc/csharp/92846.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐