.net - Files: how to distinguish file lock and permission denied cases? -
how distinguish cases when file operation failed due file lock , lack of permissions?
as per answer here: is there way check if file in use?
const int error_sharing_violation = 32; const int error_lock_violation = 33; private static bool isfilelocked(exception exception) { int errorcode = marshal.gethrforexception(exception) & ((1 << 16) - 1); return errorcode == error_sharing_violation || errorcode == error_lock_violation; } ...
filestream stream = null; try { stream = file.open(filepath, filemode.open, fileaccess.readwrite, fileshare.none); } catch (ioexception ex) { if (isfilelocked(ex)) { // know file locked } } catch (argumentnullexception ex) { handleexception(ex); } catch (securityexception ex) { handleexception(ex); } catch (argumentexception ex) { handleexception(ex); } catch (objectdisposedexception ex) { handleexception(ex); } catch (unauthorizedaccessexception ex) { // know dont have permission file handleexception(ex); } catch (exception ex) { }
Comments
Post a Comment