I have this method to copy all images from my USB to a hard-drive, say C:\
, But it throws exception for not able to access system-files/directories
.
It works fine if I skip SearchOption.AllDirectories
parameter, but my main purpose is to copy all images from USB and its sub-directories
public void Copy(string sourceDir, string targetDir)
{
try
{
var files = System.IO.Directory.GetFiles(sourceDir, "*.jpg",
SearchOption.AllDirectories);
foreach (string srcPath in files)
{
File.Copy(srcPath, srcPath.Replace(sourceDir, targetDir), true);
}
}
catch (System.UnauthorizedAccessException)
{
}
}
How can I skip system-files/directories
?