I didn't completely follow what you were saying with this one but I'll take your word for it. Currently it is setting the RootFolder to MyComputer. I have tried commenting that line out, and it changes to have Desktop at the root, rather than "This PC". Given that, I couldn't work out what you meant by the second bit regarding the original code that sets root to Desktop, or in what scenario the starting folder is always the root.
I didn't explain that very well. Let me try again.
When I run AGILE with the current code, I get this:
It always shows the 'This PC' as the starting directory, no matter what the SelectedPath property is set to. Stepping through the code, I find that in the callback function, when the BFFM_SELCHANGED message is sent during dialog initialization, the SHGetPathFromIDList call does not return a valid path.
After the dialog loads, selecting a directories by clicking also sends BFFM_SELCHANGED message. The SHGetPathFromIDList does return valid paths for these changes.
If you don't set a root directory, which means making sure the pidlRoot member of browseInfo is set to zero (by doing that explicitly, or by deleting these lines
if (_rootFolderLocation == IntPtr.Zero) {
PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner, (int)this._rootFolder, ref _rootFolderLocation);
if (_rootFolderLocation == IntPtr.Zero) {
PInvoke.Shell32.SHGetSpecialFolderLocation(hWndOwner, 0, ref _rootFolderLocation);
if (_rootFolderLocation == IntPtr.Zero) {
throw new InvalidOperationException("FolderBrowserDialogNoRootFolder");
}
}
}
from the RunDialog method), then the browser uses the default root (the user's Desktop) giving this:
With this change, stepping through the code in the BFFM_SELCHANGED message handler shows that the SHGetPathFromIDList call returns valid paths for the startup directory, which results in it being selected/displayed on startup, as shown above.
I don't know why the SHGetPathFromIDList doesn't return valid paths during startup in the first case, but does in the latter. A quick check of the Microsoft reference page for SHGetPathFromIDList shows that it's no longer supported, and links to other APIs that should be used instead. I didn't bother trying to figure that all out, since it's simpler to just use the browser's default root instead of trying to force a root.
But in summary, is this the line you are saying should be removed, from the AgileForm class:
folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
No, that by itself doesn't do the trick. If you do that you do get the Desktop be the root, but the code I mention above still runs, which forces the root to the Desktop instead of defaulting to the Desktop, giving this result:
This gives just the actual file folders in Desktop; not all the 'special folders' that are also usually accessible from the Desktop. So you can't browse to folders that are outside the Desktop folder hierarchy. And it also fails to set the starting directory (same glitch/bug as when setting This PC to be root.)
In summary, to go with the default root, delete the code I showed above. You can also delete line you referenced, and code for setting the RootFolder. And in the RunDialog method, change this line
browseInfo.pidlRoot = _rootFolderLocation;
to
browseInfo.pidlRoot = IntPtr.Zero;