我在~20%复制文件异步
中遇到了UnauthorizedAccessExceptionpublic async static Task DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, ProgressBar pb)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
float files = Directory.GetFiles(sourceDirName, "*.*", SearchOption.AllDirectories).Count();
float count = 0;
foreach (string dirPath in Directory.GetDirectories(sourceDirName, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourceDirName, destDirName));
foreach (string filename in Directory.EnumerateFiles(dirPath))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(filename.Replace(sourceDirName, destDirName)))
{
count++;
float percentage = count / files * 100;
pb.Value = (int)percentage;
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
foreach (string filename in Directory.EnumerateFiles(sourceDirName))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destDirName + filename.Substring(filename.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
和
try
{
StatusBar.Text = "Trwa kopiowanie plików...";
await CopyAsync.DirectoryCopy(game_path, installing_path, true, LowerPB);
}
catch (UnauthorizedAccessException)
{
Error.ShowError("You don't Have Permission! Run as Administrator!");
Environment.Exit(0);
}
任何建议我如何解决这个问题?我试图解决它20分钟,我不知道它是什么。 我试图以管理员身份运行,但它也不起作用。