Home > Software engineering >  How to list files in a directory that have permission to read in C#?
How to list files in a directory that have permission to read in C#?

Time:04-12

I would like to list all the files in a directory that the user has read permission. In net4, I used IO.File.GetAccessControl. But this is not available in net6. The class FileIOPermission is flagged as obsolete in net6 but it does say what replaces it. I know that when I do open the file, I will have try/catch UnauthorizedAccessException. But I don't what to open ever file just to determine whether I can. Is there a way to check file permissions in net6?

CodePudding user response:

You need a Nuget for that now called System.IO.FileSystem.AccessControl

Then you can use it like this:

var perm = new DirectoryInfo(@"myPath").GetAccessControl();

Also works for FileInfo

CodePudding user response:

Try use impersonating access

follow: https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

  • Related