首先导入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
}
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
}
BrowseForFolder,实SHBrowseForFolder的封装吧
其实我是想实现一个windows explorer左边的目录树,需要得到的是
shell名字空间的列表
我真是搞不懂,为什么微软不给Folder加上一个Path的属性???
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 );
}
这样,你就可以遍历一个目录下的所有项目。