0

What I am trying to achieve is the ability to check that we do have write access to a file share location, but without actually writing anything to it.

I know that the simplest and probably most reliable way to check you can write to a location is to just write to it. However that's not suitable here.

The scenario is this. There is a fileshare that files produced by our system get written to. Somewhere at the other end our client is picking up anything written to that folder and moving it to somewhere they want it. Every 10 minutes or so we check that we have access to the share as part of monitoring of the system. In the past we have done this by writing an empty text file. However now they are using this automated system to move files out when they are placed there this is not suitable.

So here I am, wondering is there a way to check that I can write to a location without actually writing to it.

The share we are attempting to access is a network location and requires credentials to access it.

I've tried using FileIOPermission but this says I have permission even though I don't access to the share (because I didnt log in) so when I try and actually write to it, I get an exception.

Dave
  • 2,829
  • 3
  • 17
  • 44
  • I'm not sure I understand the point. You're monitoring the share for a permissions check every 10 minutes. That must mean that you believe permissions can change at *any* time - including immediately after performing this check and before any actual write activity is attempted. What, *exactly* is this proactive check meant to save you from? – Damien_The_Unbeliever Feb 19 '18 at 17:26
  • @Damien one benefit is if you don't write anything for more than 10 min the proactive check let's you know of a network failure early before it causes a bigger problem. Think of it as a KeepAlive message. – Scott Chamberlain Feb 19 '18 at 17:43
  • @ScottChamberlain - but you still have to write a protocol that deals with problems that arise when the 10 minute check isn't sufficient and you still get an exception. I'd rather write one protocol than two. And let's not consider the opportunity for failures that are resolved before an actual problem occurs. You're now chasing problems that no longer exist, *consuming* support efforts. – Damien_The_Unbeliever Feb 19 '18 at 18:19
  • 1
    @Damien_The_Unbeliever I see your point but Scott has kind of hit the nail on the head. It's a system monitoring thing. We still handle not being able to write at the time of writing but we went to regularly check that we have write access to identify problems sooner. It's also not just permissions we need to check that the network connection to the share is still there – Dave Feb 19 '18 at 18:52

1 Answers1

1
private bool hasWriteAccessToFolder(string folderPath)
{
    try
    {
        // Attempt to get a list of security permissions from the folder. 
        // This will raise an exception if the path is read only or do not have access to view the permissions. 
        System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
        return true;
    }
    catch (UnauthorizedAccessException)
    {
        return false;
    }
}

Borrowed from : C# Test if user has write access to a folder

In this scenario, I am assuming that a FileShare is going to behave similarly to a typical Windows Directory. As far as I know - Windows does all the work in the background to treat network shares as directories, and it should use the same permission system etc.

Baaleos
  • 1,703
  • 12
  • 22
  • This works for me, I've stuck an edit in adding in a check to Directory.Exists as if it didnt exist, it threw a rather nasty exception. – Dave Feb 20 '18 at 11:16