[英]Unauthorized Access Exception - Access to the Path Denied while copying file to other Directory in C#
在 C# 中將文件從 1 個文件夾復制到另一個文件夾時出錯。 這是代碼:
string xFilename = Path.GetDirectoryName(fdlg.FileName.ToString());
string yPath = Path.GetFileName(fdlg.FileName.ToString());
upload_label.Text = xFilename + "\\" + yPath;
string zFilePath = xFilename + "\\" + yPath;
Directory.CreateDirectory("test");
try
{
File.Copy(zFilePath, "\\test", true);
}
catch(Exception eeee)
{
MessageBox.Show(eeee + "");
throw;
}
我怎樣才能解決這個問題?
MSDN (http://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx ) 說File.Copy
拋出UnauthorizedAccessException
調用者沒有所需的權限。
-或者-
destFileName 是只讀的。
請檢查上述條件並重試。
如果你有這些,它與子內容有關,就像我的情況一樣。
這就是我所做的,也許您可以嘗試以下步驟:
1.) 首先通過刪除該路徑上的文件和文件夾來刪除內容。
2.) 然后在文件夾為空后刪除文件夾本身。
var di = new DirectoryInfo("YourPath");
//delete files
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
//delete folders
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
//then delete the path itself after it is empty
Directory.Delete(path);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.