txt写入时报错出现:正由另一进程使用,原来是多此一举的操作

  //if (!File.Exists(newfilepath + "\" + name + num + ".txt"))
            //{
            //    File.Create(newfilepath + "\" + name + num + ".txt");
            //}
            string hang;
            StreamWriter sw = new StreamWriter(newfilepath + "\" + name + num + ".txt", true, System.Text.Encoding.GetEncoding("UTF-8"));

从网上找了段代码 整理了下. 原先是新建文件夹的代码. 让我改成了文件的方式,结果是多此一举, StreamWriter 方法在文件不存在的时候,会自动创建,不需要先创建空文件,注释掉上边的file之后问题解决.

原文地址:https://www.cnblogs.com/zuochanzi/p/8916137.html