c# - Check if can create folder/file in specific path -


i need check if current user has write permissions inside path. here example:

string save_path = @"c:\windows\somefolder"; string my_dir = path.directoryseparatorchar + "foobar";  //check if specific path valid if (!directory.exists(save_path)) { return; } if (directory.exists(save_path + my_dir)) { return; }  if (canwriteonpath(save_path)) {     directory.createdirectory(save_path + my_dir); } else {     //you not allowed save here or not launching application "administrator"     directory.createdirectory(@"c:\users\contoso\documents\foobar"); } 

solved in this question:

currentusersecurity cus = new currentusersecurity(); bool flag = cus.hasaccess(new directoryinfo(@"c:\windows"), filesystemrights.write);  if (flag) {     //yes create folder     directory.createdirectory(path.combine(save_path, my_dir)); } else {     //no cant     directory.createdirectory(@"c:\users\contoso\documents\foobar"); } 

the robust method try create directory , catch resulting exception.

the documentation directory.createdirectory lists possible exceptions: ioexception, unauthorizedaccessexception, argumentexception, argumentnullexception, pathtoolongexception, directorynotfoundexception.

although unlikely, possible permissions changed between code checking access allowed , trying create directory.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -