Windows Develop Bookmark and Share   
 index > Windows Forms General > Ignore Exceptions
 

Ignore Exceptions

I am currently creating an application which searches through a directory and all its subdirectories for a certain file type and then if found displays this file type in a listbox.

heres my problem however, there are certain directories/subdirectories which i do not have access to so when i run my application i get an UnauthorizedAccessException and so the application fails to complete the search, i was wondering if there was a way i could ignore this exception and just continue the search with the next directory.

I would be very grateful for any help as i have been stuck for a while with no help, and i would appreciate some useful sample code if possible...here is the code i have at the moment....

private void button1_Click(object sender, EventArgs e)

try
{

listBox1.Items.Clear();

string[] temp = Directory.GetFiles(@"C:\", "*.drl", SearchOption.AllDirectories);

for (int i = 0; i < temp.Length; i++)
{
listBox1.Items.Add(temp[i]);
}

catch (UnauthorizedAccessException)
{
throw;
}

}

}

{

SuperSav144  Thursday, September 10, 2009 8:27 AM

Hi SuperSav,
Here is the code you can use, you might have to do a bit of testing.
this code counts the number of files in "D" drive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> fileList = new List<string>();
listDirectoryNest(@"D:\",ref fileList,false);
Console.WriteLine(fileList.Count.ToString());
}

private static bool ShowACL(string FilePath)
{
try
{

FileSecurity fs = File.GetAccessControl(FilePath);

AuthorizationRuleCollection arc =

fs.GetAccessRules(true, true, typeof(NTAccount));

foreach (FileSystemAccessRule fsar in arc)
{

if ((fsar.FileSystemRights == FileSystemRights.Read) || (fsar.FileSystemRights == FileSystemRights.ReadAndExecute) || (fsar.FileSystemRights == FileSystemRights.FullControl) || (fsar.FileSystemRights == FileSystemRights.Modify))

return true;

}
return false;
}
catch (UnauthorizedAccessException E)
{
return false;
}
}

private static void listDirectoryNest(string path, ref List<string> currentList, bool includedirs)
{
if (ShowACL(path))
{
foreach (string dir in Directory.GetDirectories(path))
{
listDirectoryNest(dir + "\\", ref currentList, includedirs);
if (includedirs)
currentList.Add(dir + "\\");
}

foreach (string file in Directory.GetFiles(path))
{
currentList.Add(file);
}
}

}

}
}


Hope this helps :)

- Paras

paras kumar  Thursday, September 10, 2009 9:04 AM

Another simple way, you can use “try..catch..�to prevent application throwing an error.

Directory.GetFiles method is used to check whether this directory hase the same name file.

List<string> fileinfo = new List<string>();

private void button1_Click(object sender, EventArgs e)

{

SearchFile("f:\\", "aaa.txt");

foreach (string temp in fileinfo)

{

Console.WriteLine(temp);

}

}

public void SearchFile(string TempPath, string filename)

{

try

{

fileinfo.AddRange(Directory.GetFiles(TempPath, filename));

foreach (string temp in Directory.GetDirectories(TempPath))

{

SearchFile(temp, filename);

}

}

catch (System.Exception excpt)

{

Console.WriteLine(excpt.Message);

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 15, 2009 9:04 AM
I am currently creating an application which searches through a directory for files! i am having trouble though as i do not have read/write to some directories and subdirectories so i need to find which directories and subdirectories i don't have access to and ignore them from the search. If someone could help me that would be great. heres the code im currently using to search through the directories for a file:


string filename = textBox1.Text;
string directory = comboBox1.SelectedItem.ToString();

//searches through the selected directory for the given filename and displays each result on a new line in the listbox

string[] temp = Directory.GetFiles(@"" + directory + "", "*" + filename + "*.drl*", SearchOption.AllDirectories);

for (int i = 0; i < temp.Length; i++)
{
listBox1.Items.Add(temp[i]);
}


thankyou in advance for any help :D

SuperSav144  Tuesday, September 08, 2009 9:59 AM
Check this out :
http://social.msdn.microsoft.com/Forums/en-US/clr/thread/153c6f28-ac8f-4224-a293-a3866a5b213e


But the easiest way is to try to examine the contents of the
directory. If that works, create a temp file in the directory and delete
it. If you can do all three, you have full control. If you cannot create
the file, but you can read the contents, you have read access. If you
cannot read the directory, you have no access:D....

For theproper solution check the link above...

-Paras
  • Proposed As Answer byparas kumar Tuesday, September 08, 2009 1:38 PM
  •  
paras kumar  Tuesday, September 08, 2009 10:08 AM
yea had a look but for a novice programmer like myself it makes no sense at all! and i can't just examine every single directory and subdirectory because that would take too long
SuperSav144  Tuesday, September 08, 2009 1:07 PM
Thats why you should have a serive or a startup processrunning which will keep on finding out all the directories in which user has permission.
You can store these directories entry in some xml file or sql compact (can be anything) and later on while searching search only in the directory listed by the service.
-Paras
  • Proposed As Answer byparas kumar Tuesday, September 08, 2009 1:38 PM
  •  
paras kumar  Tuesday, September 08, 2009 1:37 PM

Hi SuperSav,
Here is the code you can use, you might have to do a bit of testing.
this code counts the number of files in "D" drive.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<string> fileList = new List<string>();
listDirectoryNest(@"D:\",ref fileList,false);
Console.WriteLine(fileList.Count.ToString());
}

private static bool ShowACL(string FilePath)
{
try
{

FileSecurity fs = File.GetAccessControl(FilePath);

AuthorizationRuleCollection arc =

fs.GetAccessRules(true, true, typeof(NTAccount));

foreach (FileSystemAccessRule fsar in arc)
{

if ((fsar.FileSystemRights == FileSystemRights.Read) || (fsar.FileSystemRights == FileSystemRights.ReadAndExecute) || (fsar.FileSystemRights == FileSystemRights.FullControl) || (fsar.FileSystemRights == FileSystemRights.Modify))

return true;

}
return false;
}
catch (UnauthorizedAccessException E)
{
return false;
}
}

private static void listDirectoryNest(string path, ref List<string> currentList, bool includedirs)
{
if (ShowACL(path))
{
foreach (string dir in Directory.GetDirectories(path))
{
listDirectoryNest(dir + "\\", ref currentList, includedirs);
if (includedirs)
currentList.Add(dir + "\\");
}

foreach (string file in Directory.GetFiles(path))
{
currentList.Add(file);
}
}

}

}
}


Hope this helps :)

- Paras

paras kumar  Thursday, September 10, 2009 9:04 AM

thankyou for the help...I have tried this but firstly im guessing it just counts the number of files in the drive which is not what i want and secondly it doesn't solve my problem as it still comes up with an error where the user doesn't have access to a subdirectory!

SuperSav144  Thursday, September 10, 2009 9:17 AM
you want to populate the listbox right??
you can get all the files from "fileList". I am counting number of files from fileList.Count only.

and second thing, here if you are getting unauthorized access errorthen your app will skip only that particular folder and will switch on to next folder.
It wont exit the program.
I think thats wat you need right.?
Do you want to set the permission also if the user doesnot have?
paras kumar  Thursday, September 10, 2009 9:22 AM
yea i want to populate the listbox with any files which end with .drl, and ok so i just have to query filelist to find my specific files? yea that is what i need, just want to move onto the next folder and carry on with the search! no i dont want need to be able to set the permission! thankyou very much for your help
SuperSav144  Thursday, September 10, 2009 9:44 AM
Hi SuperSav,

You've asked this question here: http://social.msdn.microsoft.com/Forums/en-US/clr/thread/c8c04bf0-b667-4215-9bbd-e53e08b5c228
It's not useful to double post the same question because people will not know what others have already suggested. If all the discussion is on just one thread everyone knows what's said and everyone can benefit.

Regards
Wole
Wole Ogunremi  Thursday, September 10, 2009 10:02 AM
my bad i posted it in the wrong forum the first time and didnt know how to delete it!
SuperSav144  Thursday, September 10, 2009 10:15 AM
this worked!!!! i love you
SuperSav144  Thursday, September 10, 2009 12:26 PM

Another simple way, you can use “try..catch..�to prevent application throwing an error.

Directory.GetFiles method is used to check whether this directory hase the same name file.

List<string> fileinfo = new List<string>();

private void button1_Click(object sender, EventArgs e)

{

SearchFile("f:\\", "aaa.txt");

foreach (string temp in fileinfo)

{

Console.WriteLine(temp);

}

}

public void SearchFile(string TempPath, string filename)

{

try

{

fileinfo.AddRange(Directory.GetFiles(TempPath, filename));

foreach (string temp in Directory.GetDirectories(TempPath))

{

SearchFile(temp, filename);

}

}

catch (System.Exception excpt)

{

Console.WriteLine(excpt.Message);

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, September 15, 2009 9:04 AM
paras kumar  Tuesday, September 15, 2009 11:15 AM

You can use google to search for other answers

Custom Search

More Threads

• Focused item in ListView
• Setting an proxy for the webbrowser component in VS2005 c#
• how to set color of a datagrid row (I am using .NET 1.1)
• Get File
• Problem with WS_EX_NOACTIVATE
• Clicking doesn't bring MDI windows to the front!
• List View and c#
• .mdb and C# form question for a Rich Text Box
• transparent labels
• Custom control redraw issue