I have this code to copy all files from source-directory, F:\
, to destination-directory.
public void Copy(string sourceDir, string targetDir)
{
//Exception occurs at this line.
string[] files = System.IO.Directory.GetFiles(sourceDir, "*.jpg",
SearchOption.AllDirectories);
foreach (string srcPath in files)
{
File.Copy(srcPath, srcPath.Replace(sourceDir, targetDir), true);
}
}
and getting an exception.
If I omit SearchOption.AllDirectories
and it works but only copies files from F:\
Try these steps to empty the contents of the recycle bin by deleting the files and the recycle bin folder: a. Click Start,and then type cmd in the Start Search box. b. Right-click cmd in the Programs list, and then click Run as administrator.
The folder in question is the recycle bin on that drive. And each different user has their own private recycle bin, that only they have permission to access. If anybody could access any other user's recycle bin, then users would be able to read each other's files, a clear violation of the system's security policy.
This opens elevated command prompt. c. In the elevated command prompt, type “rd /s /q C:\$Recycle.bin” without the quotes and press Enter. NOTE: For the other hard drive letters, substitute C: for the other hard drive letter instead.
There is more difficult way to use Directory.GetAccessControl () per child directory check to see if you have an access to a Directory before hand (this option is rather hard though - I don't really recommend this unless you know exactly how the access system works). Then, you only need to search/add the files among all accessible directories.
Use following function instead of System.IO.Directory.GetFiles:
IEnumerable<String> GetAllFiles(string path, string searchPattern)
{
return System.IO.Directory.EnumerateFiles(path, searchPattern).Union(
System.IO.Directory.EnumerateDirectories(path).SelectMany(d =>
{
try
{
return GetAllFiles(d,searchPattern);
}
catch (UnauthorizedAccessException e)
{
return Enumerable.Empty<String>();
}
}));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With