C# C Scan all files in the drive path

Asked 2 years ago, Updated 2 years ago, 38 views

I want to pull out the list of all the files on drive C.

I keep getting an error saying 'Access to the path has been denied'

I've also tried running with administrator privileges or raising privileges through searches, but it's not working properly

It's not working If anyone knows, please help me.

<<Source Code >>

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

            // // Display all the files.
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }

c#

2022-09-21 17:20

1 Answers

I think we need to execute the code with administrator privileges. Why don't you add the following code?

Related article

using System.Security.Principal;
public bool IsUserAdministrator()
{
    bool isAdmin;
    try
    {
        WindowsIdentity user = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(user);
        isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch (UnauthorizedAccessException ex)
    {
        isAdmin = false;
    }
    catch (Exception ex)
    {
        isAdmin = false;
    }
    return isAdmin;
}


2022-09-21 17:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.