0

I need to search few files under C:\windows folder but I am getting error on lots of folders like "Access denied".

I am admin on the system and I also tried running visual studio as administrator but nothing helped.

here is my code...

var exes = File.ReadAllLines("ListOfExes.txt");
var output = new Dictionary<string, string>();
var sb = new StringBuilder();
var seachLocation = @"c:\windows";

FileIOPermission FilePermission = new FileIOPermission(FileIOPermissionAccess.AllAccess, seachLocation);

try
{
    FilePermission.Demand();
    FilePermission.Assert();

    foreach (var exe in exes)
    {
        string[] files = Directory.GetFiles(seachLocation, exe.Trim(), SearchOption.AllDirectories);
        if (files.Length > 0 && !output.ContainsKey(exe.Trim()))
        {
            foreach (var f in files)
                Console.WriteLine(f);
        }
    }
    CodeAccessPermission.RevertAssert();
}
catch (SecurityException)
{
    Console.WriteLine("You do not have permission to read this directory.");
}

my code works fine on any other folder except windows one. If I go to the path for access denied using windows explorer then it gave me a prompt for access and if I click on continue then it let me go inside the folder.

I understand this is happening because security design but is there any way to make search in entire windows folder if I have admin rights. I need to use the code as I have thousands of file to search and note their path in windows directory.

Using windows explorer

error message

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Abhash786
  • 881
  • 2
  • 24
  • 53
  • Just because youre admin doesn't mean you have permission to see the files. Set the windows file permissions appropriately and you'll be fine – Caius Jard Feb 08 '21 at 16:43
  • run app as administrator – amirhosein adlfar Feb 08 '21 at 17:03
  • https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – itsme86 Feb 08 '21 at 17:24
  • Why are you using `FileIOPermissionAccess.AllAccess` instead of `Read`? Note also that .Net Core or C# > 7.0 doesn't support [`CodeAccessPermission`](https://learn.microsoft.com/en-us/dotnet/framework/misc/code-access-security-basics) any longer. Are you running this code from a web server? The default for a console app is all permissions, so the code does nothing. – NetMage Feb 08 '21 at 20:18
  • "it gave me a prompt for access" - what exactly did that prompt say? – NetMage Feb 08 '21 at 20:20
  • @Netmage : I have added screen shot for it – Abhash786 Feb 09 '21 at 04:29
  • I tried with Read access and it didn't work so tried with AllAccess – Abhash786 Feb 09 '21 at 04:29
  • @amirhosein adlfar Tried running app as admin but no helped. already mentioned it in my question – Abhash786 Feb 09 '21 at 04:36
  • The message seems clear - even admin users don't have access to the folder - perhaps the System account would. If you click Continue, Windows will change the security so you have access (I believe by making you owner). This can break things in Windows. On my PC I have access to CSC but not the sub-folder inside it. In .Net Core 5, you can use the `EnumerationOptions` overload to ignore inaccessible folders. – NetMage Feb 09 '21 at 18:52
  • I tested with psexec and running under the System account gave me access to CSC subfolders (CSC is Client-side Caching and is where Windows caches offline files). For .Net earlier than 5, I have a file enumeration library to ignore access denied. – NetMage Feb 09 '21 at 19:02

0 Answers0