Avoid System.Runtime.InteropServices.COMException by detecting a system folder before writing a file
I have a vb .net application where the user can choose a directory into which an excel file is written. If the user selects a system directory (yes, my users do this, don't ask me why) then a System.Runtime.InteropServices.COMException is thrown at r开发者_运维技巧untime.
I am handling this exception telling the user to please select another directory.
My application works fine as it is, but I would really like to know how to detect if the directory will allow writing a file into it before an exception in thrown.
And no, using System.IO.DirectoryInfo doesnt do the trick... or maybe it does but I am just not good enough.
Thank you!
Attempt to create a dummy file in the destination folder, which if successful you then delete - and if it (the create) fails, chances are high that you won't be able to save your .xls(x) file there! Am sure that DirectoryInfo will permit the detection of system files / folders, however, the dummy file trick is a cheap way of checking permissions on any folder.
精彩评论