lundi 25 mars 2019

Throw UnauthorizedAccessException from System.IO.Abstractions

I'm using System.IO.Abstractions (https://github.com/System-IO-Abstractions) to allow me to inject an IFileSystem into my class. I've recently added a piece of code to catch an UnauthorizedAccessException thrown when the user tries to create a file in a directory which they don't have write access to.

Is there a way the set the access permissions of a directory within System.IO.Abstractions?

The following code is as close as I have come (MockFileSystem coming from System.IO.Abstractions). The AccessControlSections description is

Specifies which sections of a security descriptor to save or load

So it appears that I need to set up a security descriptor of some sort but I can't find any information on that - or if this is even the right approach.

var mockFileSystem = new MockFileSystem();
mockFileSystem.Directory.CreateDirectory("C:\\Test", new DirectorySecurity("C:\\Test", AccessControlSections.None));

// This should throw an UnauthorizedAccessException
mockFileSystem.File.Create("C:\\Test\\File.txt");

Aucun commentaire:

Enregistrer un commentaire