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);
}
I think we need to execute the code with administrator privileges. Why don't you add the following code?
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;
}
© 2024 OneMinuteCode. All rights reserved.