0

I am trying to check if a directory has read and write permissions using .Net Core.

I am using Visual Studio 2019, .Net Core 2.1. I have tried GetAccessControl with no luck, so tried the following.

public static bool DirectoryHasPermission(string DirectoryPath)
{
    if (string.IsNullOrEmpty(DirectoryPath))
    {
        return false;
    }

    FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, DirectoryPath);
    f2.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, "C:\\example\\out.txt");

    try
    {
        f2.Demand();
    }
    catch (SecurityException s)
    {
        Console.WriteLine(s.Message);
        return false;
    }

    return true;
}

No matter what directory path I put in, even if it doesn't exist it returns true.

KJSR
  • 1,679
  • 6
  • 28
  • 51
Kev
  • 109
  • 1
  • 11
  • There are alot of examples here https://stackoverflow.com/questions/1281620/checking-for-directory-and-file-write-permissions-in-net – Esko Aug 12 '19 at 09:58
  • @Esko That's for .NET 2.0, not .NET Core 2.1. The old way doesn work in .NET Core (until 3.0). – Luaan Aug 12 '19 at 10:03
  • The relevant class in .NET is [FileSecurity](https://learn.microsoft.com/en-us/dotnet/api/system.security.accesscontrol.filesecurity?view=netframework-4.8), not FileIOPermission. Luaan already explained that the `System.Security.AccessControl` are added through the Windows-only [System.IO.FileSystem.AccessControl](https://www.nuget.org/packages/System.IO.FileSystem.AccessControl/) package. FileSecurity's doc page shows how it's used. You can access a file's permissions through `FileInfo.GetAccessControl` – Panagiotis Kanavos Aug 12 '19 at 10:06
  • @Luaan Ok, good to know. I presume that the code samples still apply after adding that package to the project? – Esko Aug 12 '19 at 10:08
  • @Kev are you looking for *Windows* or *Linux* permissions? The security models are very different and need different packages with different APIs – Panagiotis Kanavos Aug 12 '19 at 10:11
  • @Esko Yeah, it adds a couple of extension methods that essentially restore the old behaviour of "main" .NET. – Luaan Aug 12 '19 at 10:11
  • @PanagiotisKanavos Windows permissions. – Kev Aug 12 '19 at 10:16

1 Answers1

4

FileIOPermission is part of .NET's code security - it's meant to allow you to host .NET code that has lower permissions than the host process (i.e. software process isolation). It doesn't have anything to do with the file system's access rights.

For .NET Core, you can read and modify access control to files with the System.IO.FileSystem.AccessControl package. After adding the package, you can do something like this:

using System.IO;

public void Main(string[] args)
{
  var ac = new FileInfo(@"C:\Test.txt").GetAccessControl();

  // ac has the ACL for the file
}

Needless to say, this is Windows specific.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • I tried using System.IO.FileSystem.AccessControl. I added System.IO.FileSystem though NuGet, but it would not let me use it when adding using System.IO.FileSystem. Didn't recognise it! I am very new to .Net Core – Kev Aug 12 '19 at 10:11
  • @Kev The extension methods are in `System.IO`; just add the `System.IO.FileSystem.AccessControl` NuGet package, then add `using System.IO;`, and `FileInfo` should have a `GetAccessControl` method again. – Luaan Aug 12 '19 at 10:17
  • Tried to do what you said. Added the System.IO.FileSystem.AccessControl and wrote FileInfo fileInfo = FileInfo.GetAccessControl(DirectoryPath), but still doens't like GetAccessControl. – Kev Aug 12 '19 at 10:26
  • @Kev It's not a static method, you need to provide an instance of `FileInfo`, e.g. `new FileInfo(@"C:\Test.txt").GetAccessControl();`. – Luaan Aug 12 '19 at 10:29
  • GetAccessControl is not available in .Net standard 2.0 or .Net 5 – statler Apr 29 '21 at 06:46
  • @statler You need to install the `System.IO.FileSystem.AccessControl` package. It's still available for .NET 5. – Luaan Apr 29 '21 at 10:24