我正在尝试将所有文件夹复制到c:问题是它一直试图访问回收站文件夹。我试了很多方法来避免它,但都不起作用。我对系统卷信息也有同样的问题,但我只是跳过了它。我不知道为什么这种方法不适用于$recycle.bin
public void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
string[] entries;
try
{
//Gets list of all files and directories (we need it for progress bar)
entries = Directory.GetFileSystemEntries(sourceDirectory, "*", SearchOption.AllDirectories);
//entries = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories)
// .Where(d => !d.StartsWith("$RECYCLE.BIN"))
// .Where(d => !d.StartsWith("System Volume Information")).ToArray();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
Invoke(new Action(() => progressBar1.Maximum = entries.Count()));
Invoke(new Action(() => progressBar1.Step = 1));
Invoke(new Action(() => progressBar1.Value = 0)); //Using Invoke to prevent Cross thread exception
CopyAll(diSource, diTarget, entries);
}
public void CopyAll(DirectoryInfo source, DirectoryInfo target, string[] entries)
{
// lblInfo.Text = "Copying " + source;
Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
try
{
if (source.ToString() != "D:\\$RECYCLE.BIN" && source.ToString() != "System Volume Information")
{
if (!IsWorking)
{
Invoke(new Action(() => lblInfo.Text = "Stopped"));
return;
}
//Using Invoke to prevent Cross thread exception
Invoke(new Action(() => this.lblInfo.Text = string.Format("Copied {0}\\{1}", source.FullName, fi.Name)));
if (File.Exists(Path.Combine(target.FullName, fi.Name)))
{
File.Delete(Path.Combine(target.FullName, fi.Name));
}
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
Application.DoEvents();
Invoke(new Action(() => progressBar1.Value++));
}
}
catch (UnauthorizedAccessException ex)
{
// ok, so we are not allowed to dig into that directory. Move on.
}
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
try
{
if (diSourceSubDir.ToString() != "System Volume Information" && diSourceSubDir.ToString() != "$RECYCLE.BIN")
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir, entries);
Invoke(new Action(() => progressBar1.Value++));
}
else
{
Invoke(new Action(() => progressBar1.Value = progressBar1.Value + 2));
}
}
catch (UnauthorizedAccessException ex)
{
// ok, so we are not allowed to dig into that directory. Move on.
}
}我尝试了注释掉的LINQ和if语句以及大多数在线答案。我不想以管理员身份运行它,我删除了app.manifest文件,感谢您的帮助。
发布于 2019-12-19 22:13:11
下面是我所做的:我添加了条件并删除了返回;并显示了box,这样当它捕捉到一些东西时,它就会移动到下一个目录。
try
{
if (!diSource.ToString().Contains("System Volume Information") &&
!diSource.ToString().ToUpper().Contains("$RECYCLE.BIN"))
{
entries = Directory.GetFileSystemEntries(sourceDirectory, "*",
SearchOption.AllDirectories);
}
}
catch (UnauthorizedAccessException ex)
{
//ok, so we are not allowed to dig into that directory. Move on.
}https://stackoverflow.com/questions/59363673
复制相似问题