c#無法訪問文件,因為它正被另一個進程使用

大師16

我正在嘗試在創建文件後寫入文件。它無法訪問它,這是我的代碼:

                string[] name = file.Split('.');
                HttpWebRequest FileRequest = (HttpWebRequest)WebRequest.Create(URL + name[0] + ".html");
                FileRequest.UserAgent = "FSL File Getter Agent";

                using (HttpWebResponse response = (HttpWebResponse)FileRequest.GetResponse())
                using (Stream stream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(stream))
                {
                    File.Create("C:\\Users\\" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1] + "\\FSL\\" + Item + "\\" + file);
                    File.WriteAllText("C:\\Users\\" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1] + "\\FSL\\" + Item + "\\" + file, reader.ReadToEnd());
                }

這是堆棧跟踪:

The process cannot access the file 'C:\Users\Winksplorer\FSL\TestPRG\main.py' because it is being used by another process.
Stack Trace:
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding, Boolean checkHost)
   at System.IO.File.WriteAllText(String path, String contents)
   at FSL.Program.GetPKG(String URL, String Item) in Z:\FSL\Program.cs:line 85
   at FSL.Program.Main(String[] args) in Z:\FSL\Program.cs:line 30
一般

File.Create返回一個FileStream你很樂意忽略的。此操作的結果是持有打開文件的句柄,該句柄不會被及時處理。此外,您然後嘗試使用另一種方​​法File.WriteAllText使用不同的文件句柄寫入該文件,這會導致您看到的錯誤。

解決此問題的方法是使用FileStream您最初創建的,但更好的是,只需使用其中一種流CopyTo方法

using var stream = response.GetResponseStream();
using var fs = File.Create(...)
stream.CopyTo(fs);

免責聲明:這些方法有異步版本,還有許多其他方法可以實現相同的目的。這沒有經過測試,也不意味著成為完美代碼的堡壘。這只是一種致敬……簡而言之,研究您使用的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.nio.file.FileSystemException: C:\p12\dummy.p12: 進程無法訪問該文件,因為它正被另一個進程使用

列出從一個類添加的項目,而另一個類無法訪問

如何處理 IoTDB 數據庫中的“無法從 [[...]] 獲取下一個文件夾,因為它們都已滿”錯誤

無法從另一個 Java 類訪問方法

使用 onmouseover() 製作了一個 <div> display:block 但我無法點擊它,因為一旦我將鼠標移開它就會因為 onmouseout() 而消失

Java/Unix:如果另一個進程試圖在中間讀取它,一個進程能否成功寫入文件

如何使用 id 訪問另一個 html 文件中一個 html 的元素

进程无法访问文件“...”,因为它正被另一个进程使用

無法遠程訪問完美服務器

该进程无法访问文件“C:\Users\Abyss\Desktop\Test\filename.txt”,因为它正被另一个进程使用

进程无法访问该文件,因为它正被另一个进程使用 ioexception

System.IO.IOException: '进程无法访问该文件,因为它正被另一个进程使用

FileStreamResult - 进程无法访问该文件,因为它正被另一个进程使用

Python WinError 32 进程无法访问该文件,因为它正被另一个进程使用

使用 Beautifulsoup 進行網頁抓取時無法訪問 span 類

Python - 無法重新排列內核以執行另一個過程

儘管 console.log() 顯示了正確的值,但無法訪問另一個函數中函數的返回

Swift 5:成功/失敗塊中的錯誤“'catch'塊無法訪問,因為'do'塊中沒有拋出錯誤”

如何訪問和使用另一個片段中的一個片段的按鈕?

在C中如何讀取一個進程的輸出並將其寫入另一個進程的輸入中?

进程无法访问文件“filename”,因为它正被另一个进程使用。并且拒绝访问路径“文件名”

在临时目录中创建文件时,“该进程无法访问该文件,因为它正被另一个进程使用”

java.nio.file.FileSystemException: C:\test.csv -> C:\test2.csv: 该进程无法访问该文件,因为它正被 java 中的另一个进程使用

無法使用 JS 訪問我的 html 文件中的“header”元素

如何使用 Google App Script 中的“垃圾箱”將一個文件替換為另一個文件(如果它們的名稱相同)?

無法通過 html 訪問 css 文件

不能為每個架構安裝擴展,因為它聲稱它存在於那裡(它沒有)。如何進一步強迫它?

该进程无法访问该文件,因为它正被另一个进程使用” Windows 应用程序开发

File.Exists + File.Move 错误“进程无法访问该文件,因为它正被另一个进程使用。”