Simulate "access denied" for a file in C # / Windows

I am trying to write integration tests for a small subroutine C#

that reads different files.

And, well, I accidentally thought it would be great to have a test that defines the behavior for when access to that file is denied.

Does anyone know of a good and easy way to simulate this in a test sandbox?


  • I suspect this could be emulated using DirectorySecurity,

    , however I am not sure if this can be done correctly for all cases:

    Suppose, for example, I can deny access rules for the current user who is running tests (and this also requires UAC / Elevation).

    My guess is that in this case I will lose the ability to regain those rights and properly delete my sandbox after the test is complete (without diving into things like impersonation and token access manipulation).

  • I could probably do this with Moles

    or any other mock approach, but I'm more interested in a general solution (like reusing this test for my own application).

What am I missing? Any easy way to do this?

+3


source to share


1 answer


You don't specifically say you want to achieve this through your security settings, so why not get exclusive access to the file somewhere else instead?



using(File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
    //hold here to keep file locked
}

      

+4


source







All Articles