Editor Script: if (¿Path is Valid?)...

I’m writing an editor script that needs a directory location. It works fine for everything I want, and remembers the last location to Editor.prefs.

Where I’m having trouble is how to capture an error if a path is invalid.

This is the code snippet that’s giving the trouble:

			if (absolutePath == "") {
				if (EditorPrefs.HasKey ("AbsolutePath")) {
					absolutePath = EditorPrefs.GetString ("AbsolutePath");
				}
			}
			if (absolutePath == "") {
				//	Generic absolutePath that will default to the current project folder.
				absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs","","");
				// You may hard code your initial absolutePath path here: (Please note that any spaces in the absolutePath could result in errors.)
	//			absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs","/Volumes/MyHDD/MyUnityProjects/MyCurrentProject/Assets/Elements/PreFabs","");
			} else {
				absolutePath = EditorUtility.SaveFolderPanel("Select Directory to Save PreFabs", absolutePath,"");
			}
			if (!string.IsNullOrEmpty(absolutePath)) {
				pathToCreatePrefabs = absolutePath.Substring(absolutePath.IndexOf("Assets/"));
			}
			if (pathToCreatePrefabs == "") {
				pathToCreatePrefabs = "No Directory Selected";
			}
			EditorPrefs.SetString ("AbsolutePath", absolutePath);
		}

This works fine unless the saved target directory has been moved or deleted, making the absolutePath invalid… then it defaults to the system’s root directory… not even the current project file which is what you get from this line:
absolutePath = EditorUtility.SaveFolderPanel(“Select Directory to Save PreFabs”,“”,“”);

What I want to logically test is “if (absolutePath is Valid)…”, but I can’t find anything like this in the docs.

Does anyone have a workaround or suggestion on what to do to test in invalid path?

If I understand your requirements, you want to check if a path exists, rather than if the path is valid (because it contains unsupported characters like question marks or apostrophes)?

In that case, you can use the System.IO.Directory.Exists method: Directory.Exists(String) Method (System.IO) | Microsoft Learn

I’ll check that out, and yes, that’s a better description: “If path exists” rather than “If path valid”.

/me runs off to see what can be seen…