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.
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.