System.IO.IOException: file used by another process

前端 未结 10 2358
囚心锁ツ
囚心锁ツ 2020-12-14 16:59

I\'ve been working on this small piece of code that seems trivial but still, i cannot really see where is the problem. My functions do a pretty simple thing. Opens a file, c

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 17:39

    Try this: It works in any case, if the file doesn't exists, it will create it and then write to it. And if already exists, no problem it will open and write to it :

     using (FileStream fs= new FileStream(@"File.txt",FileMode.Create,FileAccess.ReadWrite))
     { 
       fs.close();
     }
     using (StreamWriter sw = new StreamWriter(@"File.txt")) 
     { 
       sw.WriteLine("bla bla bla"); 
       sw.Close(); 
     } 
    

提交回复
热议问题