Reputation: 1293
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;
}
Upvotes: 0
Views: 985
Reputation: 51683
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)
Upvotes: 1