为什么StreamWriter不写? [英] Why is the StreamWriter not writing?

查看:84
本文介绍了为什么StreamWriter不写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向名为reciept.txt的文件中写入一些文本.我已经包含了"System.IO"命名空间.当我单击保存"按钮时,它应该只在文件中写一些简单的文本.但是,当我打开文件时,它只是空白.该文件已存在.


I am trying to write a bit of text to a file called reciept.txt. I have included the "System.IO" namespace. When I click the save button it should just write that simple bit of text in the file. When I open the file however, it''s just blank. The file already exists.


private void btnSave_Click(object sender, EventArgs e)
      {
          string deskPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
          bool fileExists = File.Exists(DesktopBounds + "/reciept.txt");

          using (StreamWriter sw = new StreamWriter(deskPath+"reciept.txt"))
          {
              sw.WriteLine("asdasdasd");

          }
      }





Any suggestions why it''s not writing even when things look right?

推荐答案

只是这样修复它

just fix it like this

string deskPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            bool fileExists = File.Exists(DesktopBounds + "/reciept.txt");


            using (StreamWriter sw = new StreamWriter(Path.Combine(deskPath,"reciept.txt")))
            {
                sw.WriteLine("asdasdasd");

            }


这篇关于为什么StreamWriter不写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆