// Returns as '{static}', can I work with the wrong control?
You know that you are using the wrong control, you expected to see "ToolbarWindow32" back. A very important issue common to Codeproject.com code is that this code no longer works as published. Since 2004, Windows has changed a lot. Vista has since become the first version to add a completely new set of shell dialogs; they are based on IFileDialog . Much has improved compared to its predecessor, in particular, setting up a dialog is much cleaner using the IFileDialogCustomize interface. Actually, not what you want to do, and the settings do not include manipulation using the navigation bar.
The IFileDialogEvents interface provides the events you are looking for, this is the OnFolderChanging event . Designed so that the user can not go from the current folder, what you really want to do.
Although this looks good on paper, I must warn you that you are really trying to use these interfaces. A common problem with everything related to the Windows shell is that they simplify use only with C ++. COM interfaces are "unfriendly" types, IUnknown-based interfaces without a type library, which you can use to easily add a link to your C # or VB.NET project. Microsoft has published the Vista Bridge to use these interfaces for C #, and it looks like this . Yes Yes. The double trick, when you find that you need to do this twice, it only works in later versions of Windows, and there is a strong hint that you are trying to do this on XP (judging by the identifier of the control).
This is simply not what you want to support. Since the alternative is so simple, use the supported .NET FileOk event instead. Winforms example:
private void SaveButton_Click(object sender, EventArgs e) { string requiredDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); using (var dlg = new SaveFileDialog()) { dlg.InitialDirectory = requiredDir; dlg.FileOk += (s, cea) => { string selectedDir = System.IO.Path.GetDirectoryName(dlg.FileName); if (string.Compare(requiredDir, selectedDir, StringComparison.OrdinalIgnoreCase) != 0) { string msg = string.Format("Sorry, you cannot save to this directory.\r\nPlease select '{0}' instead", requiredDir); MessageBox.Show(msg, "Invalid folder selection"); cea.Cancel = true; } }; if (dlg.ShowDialog() == DialogResult.OK) {
Hans passant
source share