Support for File Security in .NET Core

后端 未结 3 2165
眼角桃花
眼角桃花 2021-02-14 10:10

We were porting a .NET 4.0 class Library to .NET Core 1.1 and struck with an issue of very limit support for file Security and permissions in .NET Core CLR.

We were tryin

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-14 10:59

    In dotnet standard/core:

    Import the NuGet package as user bvpb mentioned. System.IO.FileSystem.AccessControl

    Then instead of this (which only works in .NET Framework):

    FileSecurity fSecurity = File.GetAccessControl(fileName);
    

    use this (which works in all versions of .NET):

    FileSecurity fSecurity = new FileSecurity(fileName, AccessControlSections.Owner | 
                    AccessControlSections.Group |
                    AccessControlSections.Access);
    

    You may need AccessControllSections.All instead which requires the account running this code to have more permissions.

提交回复
热议问题