Path.GetDirectoryName does not know if it provided you with a folder with a point in it or a file with the extension (for example, a .txt file - a text file or a folder?).
If you know this directory, a workaround might be to do something like this.
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + "\\")
This ensures that GetDirectoryName knows that it is looking at the directory and not at the file because of the tail \ .
Updated comment based response
This problem seems specific to FolderBrowserDialog (the above information should work in other cases). I was able to reproduce your problem, and I managed to find a relatively hacky solution, but it looks like this is a bug with FolderBrowserDialog , so that should be enough.
If you set the RootFolder property RootFolder value that contains the path you entered, it works. For example, if you set RootFolder to SpecialFolders.MyDocuments , and your input is C:\...\My Documents\test.dot.folder , it should work. Thus, the workaround is done using the SpecialFolders enumeration and sets the first match.
using (FolderBrowserDialog fbd = new FolderBrowserDialog()) { fbd.SelectedPath = Path.GetFullPath(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)); //find closest SpecialFolder that matches the input (can be expanded to not be case-sensitive) foreach (var sf in Enum.GetValues(typeof(Environment.SpecialFolder))) { string spath = Environment.GetFolderPath((Environment.SpecialFolder)sf); if (fbd.SelectedPath.Contains(spath)) { fbd.RootFolder = (Environment.SpecialFolder)sf; break; } } fbd.ShowDialog(); }
source share