0

I'm trying to check a UNC file path on a NAS file share before copying files into it. I understand I may get an error on the actual copy itself (and I am since I currently don't have permission to write there), but I'd like to also check beforehand when the program launches to make let the user know whether or not they can copy files into there before attempting to do so.

My problem is that this is always returning true, when I know for certain that I don't have write permissions there as I can't copy and paste files there in File Explorer and the actual File.Copy in C# returns "Access to the path '\nascharf06\uas\to_be_processed\Andy\A.jpg' is denied".

Why does this keep returning true?

string folder = @"\\nascharf06\uas\to_be_processed\Andy";
FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Write, folder);
        try
        {
            f2.Demand();
            return true;
        }
        catch
        {
            return false;
        }
Andy
  • 1,243
  • 3
  • 22
  • 40

1 Answers1

1

Check for FileIOPermissionAcces.PathDiscovery & FileIOPermissionAccess.Read & FileIOPermissionAccess.Write - your access might be blocked because you have no accesss to even read there.

If that does not help you could use a single "dummy write" wrapped in a try - catch to make sure you can write. If you do not have delete rights, you might not be able to remove your dummy write file though.

This might help you as well: how-can-you-easily-check-if-access-is-denied-for-a-file-in-net (SO- Answers to similar question)

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I do have access to read there via C# or File Explorer. I don't have access to write and copy files there, and for now that is correct. My problem is the function in my original post returning true as if I can write there, but this should return false instead since I can neither copy files there via C# of File Explorer. I'm wondering why this is returning true instead of false like it should. – Andy Dec 29 '17 at 23:53
  • @Andy - just for giggles, take a file from there and save it locally. modify it and copy it back. My guess is you have WRITE but not CREATE permissions - keep an unmodified copy aside so you can copy that over your modified one – Patrick Artner Dec 29 '17 at 23:56