C# Program crashes when Writing using StreamWriter

蹲街弑〆低调 提交于 2019-12-02 10:20:30

The error here is inside your Btn_Create_Click. You are using File.CreateText without disposing the stream. Take a look here.

Just call Dispose or just place it inside a Using.

Like so:

private void Btn_Create_Click(object sender, EventArgs e)
{
    string path = Environment.CurrentDirectory + "/"+ "File.txt";
    if (!File.Exists(path))
    {
        File.CreateText(path).Dispose();
        MessageBox.Show("File Created Successfully");
    }
    else
    {
        MessageBox.Show("File Already Created");
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!