0

I am making a software with Winforms and C# 7.0. I have to get all the files in C:\Program Files and C:\Program Files (x86). When I try Directory.GetFiles(@"C:\Program Files\", "*.*", SearchOption.AllDirectories I get an exception that the access to the path C:\Program Files\Common Files' is denied.

I tried to start my program as an administrator but it still doesn't work. How can I get a list of all files in access-denied folders and read them?

Kind regards

Adam Miklosi
  • 764
  • 5
  • 18
  • 28
Owns_Rekt
  • 164
  • 11
  • Possible duplicate of [Why is access to the path denied?](https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied) – TAHA SULTAN TEMURI Sep 22 '18 at 08:02
  • The answers at this question don't work too – Owns_Rekt Sep 22 '18 at 08:07
  • add app menifest file to your project use run as admin in configuration,or start visual studio as admin ,make sure you have administrative rights – TAHA SULTAN TEMURI Sep 22 '18 at 08:10
  • Your program (or you) don't have the appropriate permissions to read those directories. You need to change that, or design your method in a way to skip the directories that you cant read – TheGeneral Sep 22 '18 at 08:37
  • First of all I NEED to read the directories. Second I have administrative rights. I also changed the run as admin config in my app.manifest – Owns_Rekt Sep 25 '18 at 14:42

1 Answers1

0

You will have to skip the directories you can't read (assuming that you can't run your program under the System account or other account with privileges to read all directories).

You have to be careful here because you can't use yield inside a try/catch. Here's one approach:

public static IEnumerable<string> EnumFilesRecursively(string rootDirectory)
{
    // Helper method to call GetEnumerator(), returning null on exception.

    IEnumerator<T> getEnumerator<T>(Func<IEnumerable<T>> getEnumerable)
    {
        try   { return getEnumerable().GetEnumerator(); }
        catch { return null; }
    }

    // Enumerate all files in the current root directory.

    using (var enumerator = getEnumerator(() => Directory.EnumerateFiles(rootDirectory)))
    {
        if (enumerator != null)
            while (enumerator.MoveNext())
                yield return enumerator.Current;
    }

    // Recursively enumerate all subdirectories.

    using (var enumerator = getEnumerator(() => Directory.EnumerateDirectories(rootDirectory)))
    {
        if (enumerator != null)
            while (enumerator.MoveNext())
                foreach (var file in EnumFilesRecursively(enumerator.Current))
                    yield return file;
    }
}

To test it:

public static void Main(string[] args)
{
    foreach (var file in EnumFilesRecursively(@"C:\Program Files\"))
    {
        Console.WriteLine(file);
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • @Owns_Rekt In that case you will need to run your application with System credentials, otherwise it will not be permitted to enumerate the directory contents. – Matthew Watson Sep 25 '18 at 15:31