簡體   English   中英

File.Copy 阻止 File.WriteAllText - 該進程無法訪問該文件,因為它正被另一個進程使用

[英]File.Copy prevents File.WriteAllText - The process cannot access the file because it is being used by another process

我有一個循環

  • 刪除一個名為“delete_me”的文本文件(成功)
  • 將新文件fileName重命名為“delete_me”(成功)

...然后我試圖 append 一個字符串到那個新重命名的文件,但我不能......

The process cannot access the file 'delete_me.txt' because it is being used by another process.

    File.Delete("delete_me.txt");
    File.Copy(fileName, "delete_me.txt");
    File.WriteAllText("delete_me.txt", "some string");

我知道有一些Dispose()Close()方法可以掛在File.Create()的后面,但這似乎不適用於File.Copy()

有任何想法嗎?

我試過GC.Collect()但仍然得到同樣的錯誤。

基於MS 文檔

public static void Copy (string sourceFileName, string destFileName);

sourceFileName要復制的文件。

destFileName目標文件的名稱。 不能是目錄或現有文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM