日期:2014-05-19  浏览次数:20858 次

关于(C#)Windows Shell 外壳编程的问题
摘抄其中一段用来在TreeView中显示文件夹结构

private   void   tvwExplorer_BeforeExpand(object   sender,   System.Windows.Forms.TreeViewCancelEventArgs   e)
{
try
{
#region  
if   (e.Node.Nodes.Count   !=   1)
{
return;
}
else
{
if   (e.Node.FirstNode.Text   !=   "... ")
{
return;
}
}

e.Node.Nodes.Clear();
#endregion

ShellItem   sItem   =   (ShellItem)e.Node.Tag;
IShellFolder   root   =   sItem.ShellFolder;

IEnumIDList   Enum   =   null;
IntPtr   EnumPtr   =   IntPtr.Zero;
IntPtr   pidlSub;
int   celtFetched;

if   (root.EnumObjects(this.Handle,SHCONTF.FOLDERS,   out   EnumPtr)   ==   API.S_OK)
{
Enum   =   (IEnumIDList)Marshal.GetObjectForIUnknown(EnumPtr);
while   (Enum.Next(1,   out   pidlSub,   out   celtFetched)   ==   0   &&   celtFetched   ==   API.S_FALSE)
{
string   name   =   API.GetNameByIShell(root,   pidlSub);
//string   path   =   API.GetPathByIShell(root,   pidlSub);
IShellFolder   iSub;
root.BindToObject(pidlSub,   IntPtr.Zero,   ref   Guids.IID_IShellFolder,   out   iSub);

ShellItem   shellItem=new   ShellItem(pidlSub,   iSub,   sItem);
int   imgIndex   =   API.GetSmallIconIndex(shellItem.PIDLFull.Ptr);
TreeNode   nodeSub   =   new   TreeNode(name,   imgIndex,   imgIndex);

nodeSub.Tag   =   shellItem;
nodeSub.Nodes.Add( "... ");
e.Node.Nodes.Add(nodeSub);
}
}                        
}
catch(Exception   ex)
{
throw   ex;
}
}

以上代码可以显示目录树结构,但出现如果当前节点中(当前文件夹中)如果没有文件(任意类型文件)
//string   strPath   =   API.GetPathByIShell(root,   pidlSub);就取不到路径   如果文件夹中有文件存在就能取到路径,请赐教如何解决,谢谢!

摘抄:http://www.cnblogs.com/lemony/archive/2007/04/17/716112.html

------解决方案--------------------
要解决什么?
------解决方案--------------------
不用Windows API也可以吧 去codeproject上搜搜 有现成的例子
------解决方案--------------------
顶顶顶