Get the exception folder to a listbox C# - c#

I'm creating a method to handle directory and file exceptions.
I have listed all the directories and subdirectories of the C: \ Users \ folder
listBox1.Items.AddRange(Directory.GetDirectories("C:\\Users\\", "*", SearchOption.AllDirectories));
Some folders are protected by windows, and when you run a command for those folders it gives us an exception.
If there is any possible, How can I save the folder that returned the exception to a listbox when the exception happens? thanks

If you catch the Exception the Message will tell you the folder that threw the exception in the format of
Access to the path 'C:\Users\SomeFolder' is denied.
you just need to remove the static text, the simplest way is(you maybe able to find a more stylish way to do this, but you get the idea):
try
{
listBox1.Items.AddRange(Directory.GetDirectories("C:\\Users\\", "*", SearchOption.AllDirectories));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SomeListBox.Items.Add(ex.Message.Replace("Access to the path '", "").Replace("' is denied.", ""));
}
EDIT: if you want to traverse all the folders and keep going you might want to take a look at recursion. you could slightly modify the above code to make it a function.
void getDirectories(string path)
{
try
{
string[] directories = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
listBox1.Items.AddRange(directories);
foreach(string directory in directories)
{
getDirectories(directory);
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
SomeListBox.Items.Add(ex.Message.Replace("Access to the path '", "").Replace("' is denied.", ""));
}
}

In case you want all folders with exceptions:
You can use the method with the following call:
List<DirectoryInfo> list = GetDirectories(new DirectoryInfo(#"root directory"), 0, 2);
I've implemented a max level, because in a deep structure it could take a long time before the search is done. If you don't need it, just remove the unnecessary lines of code ;)
List<DirectoryInfo> GetDirectories(DirectoryInfo di, int level, int maxLevel)
{
List<DirectoryInfo> exceptionList = new List<DirectoryInfo>();
foreach (DirectoryInfo directoryInfo in di.GetDirectories("*", SearchOption.TopDirectoryOnly))
{
try
{
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
if (level + 1 < maxLevel)
{
exceptionList.AddRange(GetDirectories(directoryInfo, level + 1, maxLevel));
}
}
catch (UnauthorizedAccessException)
{
exceptionList.Add(directoryInfo);
}
}
return exceptionList;
}

Related

C# delete all the things inside the file

I'm trying to delete some files from a specific directory. But there are some errors.
I want my program to ignore these problems.
string[] myFiles = Directory.GetFiles(#"C:\Windows\prefetch");
foreach (string f in myFiles)
{
File.Delete(f);
}
By "errors" you mean thrown exceptions? Directory.GetFiles and File.Delete will throw exceptions if they can't find the directory/file or if you dont have permissions to access it, etc.
Since you want them ignored, you can just catch them and ignore them.
try
{
string[] myFiles = Directory.GetFiles(#"C:\Windows\prefetch");
foreach (string f in myFiles)
{
File.Delete(f);
}
}
catch (Exception)
{
//do nothing
}
use this and you will delete the entire directory and respective files recursively
Directory.Delete(#"C:\Windows\prefetch", true);

Delete path that is longer than 256 characters using DirectoryInfo.Delete(true) method in c#

My Goal is to use Delete the entire folder and everything inside of that folder that is older than month. Following is code that i wrote:
foreach (String dir in Directory.EnumerateDirectories(args[0]))
{
foreach (String subDir in Directory.EnumerateDirectories(dir))
{
DirectoryInfo dirInfo = new DirectoryInfo(subDir);
if (dirInfo.CreationTime < DateTime.Today.AddMonths(-1))
{
try
{
dirInfo.Delete(true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Isssue: dirInfo.Delete(true) statement throws an exception when path is longer than 256 characters, and does not delete the folder. How can i over come that?
Thank you
You could try moving the folder first to a shorter directory path first so its path is no longer as long, and then deleting it.
dirInfo.MoveTo("C:\temp");
dirInfo.Delete(true);

UnauthorizedAccessException with getDirectories

Hello everyone I currently got subdirectories I wanted through this call:
foreach (DirectoryInfo dir in parent)
{
try
{
subDirectories = dir.GetDirectories().Where(d => d.Exists == true).ToArray();
}
catch(UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
foreach (DirectoryInfo subdir in subDirectories)
{
Console.WriteLine(subdir);
var temp = new List<DirectoryInfo>();
temp = subdir.GetDirectories("*", SearchOption.AllDirectories).Where(d => reg.IsMatch(d.Name)).Where((d => !d.FullName.EndsWith("TESTS"))).Where(d => !(d.GetDirectories().Length == 0 && d.GetFiles().Length == 0)).Where(d => d.GetFiles().Length > 3).ToList();
candidates.AddRange(temp);
}
}
foreach(DirectoryInfo dir in candidates)
{
Console.WriteLine(dir);
}
so now my issue is that my final list called candidates I get nothing because im getting an access issue due to one of the folders called lost+found in my subdirectories folder in the try block. I tried using try and catch to handle the exception so I could keep doing my checks I actually dont care about this folder and im trying to just ignore it but I'm not sure how to go about ignoring it out of my get directories search any thoughts? I already tried doing a filter with .where to ignore any folder that contained the folder name but that didnt work either it just stopped my program at the folder name.
I have the same question (ResourceContext.GetForCurrentView call exception) about this exception (UnauthorizedAccessException), and this link gives an answer to the reason why this happens:
http://www.blackwasp.co.uk/FolderRecursion.aspx
Short quote:
... Key amongst these is that some of the folders that you attempt to
read could be configured so that the current user may not access them.
Rather than ignoring folders to which you have restricted access, the
method throws an UnauthorizedAccessException. However, we can
circumvent this problem by creating our own recursive folder search
code. ...
solution:
private static void ShowAllFoldersUnder(string path, int indent)
{
try
{
foreach (string folder in Directory.GetDirectories(path))
{
Console.WriteLine("{0}{1}", new string(' ', indent), Path.GetFileName(folder));
ShowAllFoldersUnder(folder, indent + 2);
}
}
catch (UnauthorizedAccessException) { }
}
You can use recursion like Microsoft explains: link.

Access to the path 'c:\$Recycle.Bin\S-1-5-18' is denied

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:\
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>();
}
}));
}
File system objects are subject to security. Some file system objects are secured in such a way that they can only be accessed by certain users. You are encountering a file to which the user executing the code does not have sufficient rights to access.
The reason that you don't have access rights for this particular folder is to protect the security of the different users on the system. 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.
Perhaps the simplest way around this is to skip hidden folders at the root level of the drive. That simple change would be enough to solve your problem because you surely don't want to copy recycle bins.
That folder is a secure system folder (your bin, each drive has its own bin). Just place your file.copy into a try catch statement and ignore/log all the failures. That way you will only copy actual files and skip system files/folders.
If you really want to avoid the try catch statement. Use the fileinfo and directory info classes to figure out which folders/files are of the system and will throw an exception.
This should do the trick:
private IEnumerable<string> RecursiveFileSearch(string path, string pattern, ICollection<string> filePathCollector = null)
{
try
{
filePathCollector = filePathCollector ?? new LinkedList<string>();
var matchingFilePaths = Directory.GetFiles(path, pattern);
foreach(var matchingFile in matchingFilePaths)
{
filePathCollector.Add(matchingFile);
}
var subDirectories = Directory.EnumerateDirectories(path);
foreach (var subDirectory in subDirectories)
{
RecursiveFileSearch(subDirectory, pattern, filePathCollector);
}
return filePathCollector;
}
catch (Exception error)
{
bool isIgnorableError = error is PathTooLongException ||
error is UnauthorizedAccessException;
if (isIgnorableError)
{
return Enumerable.Empty<string>();
}
throw error;
}
}

How to delete files while Traversing folder tree

I am not sure if I am doing this correctly or if my logic is correct.
I am trying to go down a folder structure delete files older than a certain number of days, this part I have correctly implemented, delete empty folders.
Can all this be done in one loop?
Where do I do the folder delete?
I want to delete empty folders up to 3 or 4 level down.
private static void TraverseTree(System.IO.DirectoryInfo folder, double days)
{
Stack<string> dirs = new Stack<string>();
if (!folder.Exists)
throw new ArgumentException();
dirs.Push(folder.FullName);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
// Perform the required action on each file here.
// Modify this block to perform your required task.
foreach (string file in files)
{
try
{
// Perform whatever action is required in your scenario.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
// Delete old files
if (fi.LastWriteTime < DateTime.Now.AddDays(-days))
fi.Delete();
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
// or thread since the call to TraverseTree()
// then just continue.
Console.WriteLine(e.Message);
continue;
}
}
// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
dirs.Push(str);
}
}
Code is from MSDN.
A recursive approach would probably be cleaner.
private static void DeleteOldFilesAndFolders(string path)
{
foreach (string directory in System.IO.Directory.GetDirectories(path))
{
DeleteOldFilesAndFolders(directory);
// If the directory is empty and old, delete it here.
}
foreach (string file in System.IO.Directory.GetFiles(path))
{
// Check the file's age and delete it if it's old.
}
}
Something that I notice about your code is that the dozens of lines of "mechanism" for walking the tree structure completely overwhelms the two lines of code that actually do the work. That makes it hard to read, understand, change, debug and maintain this code.
Here's what I would do instead.
You have only three high-level operations in your program: (1) get all the files, (2) filter to find the ones to delete, (3) delete each file. So write a program which does each of those in one statement.
For the first operation, I would factor out the mechanism above into its own function: a function which implements, say, IEnumerable, and all it does is keeps on yielding out information about files. It doesn't do anything with them; its only purpose is to keep spitting out file infos.
Once you have that mechanism then you can start writing queries on top of that sequence to attain the second operation. The third operation then follows directly from the second.
In short, the main line of your program should look something like this:
var allFiles = TraverseFolder(folder);
var filesToDelete = from file in allFiles where IsOld(file) select file;
foreach(var fileToDelete in filesToDelete) Delete(fileToDelete);
Is that clear?
There is almost the same question here:
How to delete all files and folders in a directory?
This is delete by name, but you could check other properties.
Here is a more general solution to the problem which gives you a file system walker implemented non-recursively as IEnumerable.
for which your solution can probably be implemented as:
List<string> directoriesToDelete = new List<string>();
DirectoryWalker walker = new DirectoryWalker(#"C:\pathToSource\src",
dir => {
if (Directory.GetFileSystemEntries(dir).Length == 0) {
directoriesToDelete.Add(dir);
return false;
}
return true;
},
file => {
if (FileIsTooOld(file)) {
return true;
}
return false;
}
);
foreach (string file in walker)
File.Delete(file);
foreach (string dir in directoriesToDelete)
Directory.Delete(dir);
I enhanced John's solution, implementing missing code, error handling and checks:
/* Given a directory path and a datetime,
* recursively delete all files and directories contained in such directory
* (given directory included) that are younger than the given date.
*/
private bool DeleteDirectoryTree(string dir, DateTime keepFilesYoungerThan)
{
//checks
if (String.IsNullOrEmpty(dir) || !Directory.Exists(dir))
return false;
//recurse on children directories
foreach (string childDir in Directory.GetDirectories(dir))
DeleteDirectoryTree(childDir, keepFilesYoungerThan);
//loop on children files
foreach (string file in Directory.GetFiles(dir))
{
//calculate file datetime
DateTime fileDT = new DateTime(Math.Max(File.GetCreationTime(file).Ticks, File.GetLastWriteTime(file).Ticks));
//if file is old, delete it
if (fileDT <= keepFilesYoungerThan)
try
{
File.Delete(file);
Log("Deleted file " + file);
}
catch (Exception e)
{
LogError("Could not delete file " + file + ", cause: " + e.Message);
}
}
//if this directory is empty, delete it
if (!Directory.EnumerateFileSystemEntries(dir).Any())
try
{
Directory.Delete(dir);
Log("Deleted directory " + dir);
}
catch (Exception e)
{
LogError("Could not delete directory " + dir + ", cause: " + e.Message);
}
return true;
}

Categories