Checking folder read write access C#
-
hi..... I am working on a small application using C3, VS 2003. I have to check if the folder has read or write permissions to it, before i generate my output in that folder.VS 2005 has some functions for checking the folder access, but i dint get any for VS 2003. Please do let me know if there is any function check the folder permissions in VS 2003. Thnx in advance.
-
hi..... I am working on a small application using C3, VS 2003. I have to check if the folder has read or write permissions to it, before i generate my output in that folder.VS 2005 has some functions for checking the folder access, but i dint get any for VS 2003. Please do let me know if there is any function check the folder permissions in VS 2003. Thnx in advance.
FileIOPermission
is there since .NET 1.1. Use the Demand method to check you have access to folders. Check the following code (not tested on .NET 1.1)bool CanWriteToFolder(string folder)
{
FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, folder);
try {
f.Demand();
}
catch (SecurityException) {
return false;
}
return true;
}Check documentation[^] for detailed examples.
Navaneeth How to use google | Ask smart questions
-
FileIOPermission
is there since .NET 1.1. Use the Demand method to check you have access to folders. Check the following code (not tested on .NET 1.1)bool CanWriteToFolder(string folder)
{
FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, folder);
try {
f.Demand();
}
catch (SecurityException) {
return false;
}
return true;
}Check documentation[^] for detailed examples.
Navaneeth How to use google | Ask smart questions
-
for some reason this doesn't work for me. The SecurityException is never thrown.
-
for some reason this doesn't work for me. The SecurityException is never thrown.
I have the same problem I think the reason is that this is fileIO permissions. Not folder. Still looking for a folder check - will update if I find something.
-
I have the same problem I think the reason is that this is fileIO permissions. Not folder. Still looking for a folder check - will update if I find something.
Hi, You can try following code block to check if the directory is having Write Access. It checks the FileSystemAccessRule.
string directoryPath = "C:\\XYZ"; //folderBrowserDialog.SelectedPath; bool isWriteAccess = false; try { AuthorizationRuleCollection collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); foreach (FileSystemAccessRule rule in collection) { if (rule.AccessControlType == AccessControlType.Allow) { isWriteAccess = true; break; } } } catch (UnauthorizedAccessException ex) { isWriteAccess = false; } catch (Exception ex) { isWriteAccess = false; } if (!isWriteAccess) { //handle notifications }
-
Hi, You can try following code block to check if the directory is having Write Access. It checks the FileSystemAccessRule.
string directoryPath = "C:\\XYZ"; //folderBrowserDialog.SelectedPath; bool isWriteAccess = false; try { AuthorizationRuleCollection collection = Directory.GetAccessControl(directoryPath).GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); foreach (FileSystemAccessRule rule in collection) { if (rule.AccessControlType == AccessControlType.Allow) { isWriteAccess = true; break; } } } catch (UnauthorizedAccessException ex) { isWriteAccess = false; } catch (Exception ex) { isWriteAccess = false; } if (!isWriteAccess) { //handle notifications }
This work on local computer, how about a network shared folder(e.g.\\Server01\Files)?
I am happy to work with people doing great projects.