I’m using Microsoft Visual Basic 2008 Express Edition. I have a Folder Dialog Browser added in my form and I call it when the user presses a button. The folder path string is stored in a string variable and displayed as text in a text box. (maybe I should use a combo box)
My question is: If the user inputs by hand a non existent path, how do I return an error message, stop the file creation into the invalid path, and return to my main form?
Use the
Directory.Existsmethod from theSystem.IOnamespace:Note that, due to various circumstances (e.g. the directory is removed between
Directory.Existsand your file creation code, or the user does not have permissions to write to that directory, or the directory is on a network drive and the network connection suddenly fails, …), your file creation might fail nonetheless. Thus, it might still make sense to catch the error (usingTry ... Catch) and provide a meaningful error message to the user (instead of letting your application “just crash”).