# [SOLVED] [C# Net] insufficient Privileges



## Emphyrio (May 16, 2009)

I'm trying to write a program that searchs for a specific file in all Directories and sub directories.
Target: All CPU / Net 4.0 Framework


```
public string SearchFile ()
        {
            FileIOPermission ioPerm = new FileIOPermission(PermissionState.Unrestricted);
            string sd = Environment.GetEnvironmentVariable("SystemDrive");
            String sf = "test.txt"; // Test file placed in c:\  and C:\Users\Patrick
            string[] filePaths = Directory.GetFiles (sd, sf, SearchOption.AllDirectories);
            ioPerm.AddPathList(FileIOPermissionAccess.Read, filePaths);
            try
            {
                foreach (string p in filePaths)
                {
                    MessageBox.Show(p);

                }
            }
            catch (System.Exception excpt)
            {
                MessageBox.Show(excpt.ToString());
            }

           return "" ;
        }
```
I am getting the " Make sure you have sufficient privileges in C:\Documents and settings...." error on this line:


```
string[] filePaths = Directory.GetFiles (sd, sf, SearchOption.AllDirectories);
```
I have a custum app.manifest with elevated adm rights.


```
<requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
```
I've search Google, read a lot of MSDN / TechNet tips and info, tried different search approaches (recursieve/iteration), different FileIOPermission settings,... nothing seems to work. :sigh:

Can anyone throw a light on this ?


----------



## niemiro (Feb 20, 2010)

*Re: [C# Net] insufficient Privileges*

Hello!

As you have probably read, the Documents and Settings "folder" is actually not a folder at all, but actually a symbolic link. You still receive this permissions message, but actually, this is not a permissions problem at all, but a program which cannot read Symbolic Links. Since you will later search C:\Users, the solution is not to make the program able to read symbolic links (otherwise you will waste time searching C:\Users twice), but rather catch the exception, and continue onwards, past that folder. Therefore, your code for folders without permissions is good, but this isn't exactly a permissions issue, hence why it isn't working.

Unfortunately, Directory.GetFiles will quit out as soon as it comes across a folder it can't open. There is not solution for this. And it is bloomin' annoying!

Therefore, you must manually recurse (take each directory, take each file, and then in each directory, search for directories and file, and in each of those directories...) and for each directory, enclose in a Try...Catch, and continue onwards if you fail to open the directory.

If you can write this, then great (it really isn't too hard). However, if you need inspiration, some kind people have put examples online for you to use. I will show you two good ones, which are hard to find, but better than the rest.

C# Recursive File and Directory Method

That one is very simple. It is also very easy to understand. However, remember that none are absolutely perfect for any one scenario.

Personally, I wouldn't use a plain Catch (that is a VERY bad idea, for many reasons) Probably better to change that to Catch (System.UnauthorizedAccessException)

Also, if you wish, you could easily add a pattern search, etc.

Also, can I use this time to make a general comment which apply to all snippets? If you know a little bit of OOP, create a Strategy Design Pattern template to test your algorithms on, and also, a little knowledge of LINQ could speed things up quite a bit here...

Also, if you are worried that some directories might actually have permissions problems (actual permissions problems, and not Symbolic Links) (there are some Norton directories which spring to mind) then implement some of your above code. Keep in mind efficiency (very important if you are searching an entire hard disk drive) and most likely you don't have to deal with permissions for every file, just when you fail to open one.

Also, if you are searching for one file, the most specific the pattern, the shorter the search time. USE PATTERNS! If you are going to search for multiple files, obviously, use an all inclusive pattern, and search the vector.

Remember to put this onto a BackgroundWorkerThread (or any other thread) so as not to cause *too* much harm to the UI thread, and use delegates to call back to your UI.

Have a look at this snippet (if you know enough C# keywords to understand it) Use Iterators to get all files (recursive) - C# - Source Code | DreamInCode.net

This one has some efficiencies, and some inefficiencies (if maybe you don't need all of the data in a DirectoryInfo object), but nevertheless is very nice.

It also needs a fair amount more work to make it do as you wish it to...

Finally, if you want to recurse deep, beyond the 260 character MAX_PATH limit, tell me, and I shall show you another way 

I really hope that this helps..., and the very best of luck!

Richard

P.S. Feel free to ask if I have used any unfamiliar terminology! Your best bet would probably be to use the top link, and then follow the rest of the post as and when and if you run into problems (such as the MAX_PATH limitation)


----------



## Emphyrio (May 16, 2009)

*Re: [C# Net] insufficient Privileges*

Hi niemiro,

Lots of thanks for Your exellent clarification.
This realy was very usefull. ray:

It has set my on my way 


I haven't much experience with Strategy Design Pattern template or LINQ, but I will take Your good advice in consideration and try to get it done.

I come from old skool ASM programming (80x86 and 6502C) to OOP, it was (at first) a bit confusing (for me).
But the more I use OOP (and VS 2010) the more I come too like it.

The link too DreamCode (iteration) was one I came across, but Catch (System.UnauthorizedAccessException) wasn't in my mind.
It all makes sence now.


Again, my gratefully thank too You to have taking the time for explaning.
It did help me a lot.

Emphyrio


----------



## niemiro (Feb 20, 2010)

No problem! It has been an absolute pleasure to work with you! Really glad to have been of assistance, and good luck with your program!

Richard


----------

