首先导入shell32 com组件string    folderToReturn = null;
try {
Shell32.Shell shell = new Shell32.Shell ();
Shell32.Folder folder            = shell.BrowseForFolder(this.Handle.ToInt32(), "Select a folder", 8, "");
Shell32.FolderItems folderItems = folder.Items();
Shell32.FolderItem    folderItem  = folderItems.Item(0);
String    fullPath                = folderItem.Path;
// Now parse out the unecessary final part
fullPath    = fullPath.Substring(0, fullPath.LastIndexOf(@"\"));
folderToReturn = fullPath;
}catch {
// An error occured. Just igonre it, null will be returned - we called a silly DLL anyway
}

解决方案 »

  1.   

    谢谢,我想不用对话框,怎么实现
    BrowseForFolder,实SHBrowseForFolder的封装吧
    其实我是想实现一个windows explorer左边的目录树,需要得到的是
    shell名字空间的列表
      

  2.   

    heroux(FlyerAero) 的做法有问题,如果这个folder没有子目录和文件,则folderItem就是null,那样就会抛出异常了。
    我真是搞不懂,为什么微软不给Folder加上一个Path的属性???
      

  3.   

    你可以这么做:
    Shell32.ShellClass shell = new Shell32.ShellClass();
    Shell32.Folder folder = shell.Namespace(/*Your dir name*/);
    foreach(Shell32.FolderItem folderItem in folder.Items() )
    {
      MessageBox.Show( folderItem.Path );
    }
    这样,你就可以遍历一个目录下的所有项目。