How to delete a file after checking whether it exists

后端 未结 10 1835
盖世英雄少女心
盖世英雄少女心 2020-11-30 19:50

How can I delete a file in C# e.g. C:\\test.txt, although apply the same kind of method like in batch files e.g.

if exist \"C:\\test.txt\"

dele         


        
10条回答
  •  鱼传尺愫
    2020-11-30 20:10

    If you are reading from that file using FileStream and then wanting to delete it, make sure you close the FileStream before you call the File.Delete(path). I had this issue.

    var filestream = new System.IO.FileStream(@"C:\Test\PutInv.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
    filestream.Close();
    File.Delete(@"C:\Test\PutInv.txt");
    

提交回复
热议问题