[英]C# ASP.NET File.OpenRead from UNC path: “Access to path…is denied.”
[英]why File.copy works but File.OpenRead prompts access denied?
我想復制另一個進程正在使用的加密文件。
這有效:
System.IO.File.Copy("path1", "path2",true);
但是下面的代碼不起作用。 提示“文件訪問被拒絕”錯誤:
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))//access denied open file
{
using (Stream copyFileStream = new StreamDecryption(new FileStream(copyTo, FileMode.Create)))
{
}
}
如果另一個進程使用文件,我如何復制加密文件?
謝謝
更新 :我使用此代碼並為我工作:
using (var fileStream = new System.IO.FileStream(@"filepath", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
{
}
如果您使用FileShare.Read
(在您的示例中隱式發生),如果另一個進程已經打開文件進行寫入,則打開該文件將失敗。
File.OpenRead(fileName)
new FileStream(fileName, FileMode.Open, FileAccess.Read)
new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
如果指定FileShare.ReadWrite
,則在打開時不會觸發錯誤,但另一個進程可能會在您閱讀時更改您正在閱讀的數據。 您的代碼應該能夠處理未完成的數據或此類更改。
new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.