是不是和在Windos 系统中一样,从shell32.dll里面扩展出来?还是有另外的办法??各位大侠帮忙啊

解决方案 »

  1.   

    下面是调用API实现的“选择文件夹”:using System.Runtime.InteropServices;
    using System.Text;#region BrowseFolderClass
    public class BrowseFolderClass
    {
        /*
        private struct SHITEMID
        {
         ushort cb;
         byte adID; // BYTE abID[1];
        }
        private struct ITEMIDLIST
        {
         SHITEMID mkid;
        }
        */
        [StructLayout(LayoutKind.Sequential)]
            private struct BROWSEINFO
        {
            public IntPtr hwndOwner; // HWND hwndOwner
            public IntPtr pidlRoot; // const ITEMIDLIST* pidlRoot
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pszDisplayName;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszTitle;
            public int ulFlags;
            [MarshalAs(UnmanagedType.FunctionPtr)]
            public BrowseCallBackProc lpfn;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lParam;
            public int iImage;
        }    [DllImport("User32.Dll")]
        [return : MarshalAs(UnmanagedType.Bool)]
        private extern static bool SendMessage( IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPTStr)] string lParam );
        [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
        private extern static IntPtr SHBrowseForFolder( ref BROWSEINFO lpbi );
        [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
        [return : MarshalAs(UnmanagedType.Bool)]
        private extern static bool SHGetPathFromIDList( IntPtr pidl, StringBuilder pszPath );
        private delegate int BrowseCallBackProc( IntPtr hwnd, uint msg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData );
        private static int BrowseCtrlCallback( IntPtr hwnd, uint uMsg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData )
        {
            if( uMsg == 1 ) // BFFM_INITIALIZED
            {
                string szPath = lpData;
                if( szPath!=null && szPath!="" )
                {
                    if( szPath[szPath.Length-1] != '\\' )
                        szPath += '\\';
                    SendMessage( hwnd, 0x400+103, 1, szPath ); // BFFM_SETSELECTION
                }
            }        return 0;    }    // hwndOwner£º¸¸´°Ìå¾ä±ú
        // Title£º±êÌ⣻¿ÉÒÔΪnull£¬Ò²¿ÉÒÔΪ""
        // lpszInitPath£º³õʼĿ¼£»¿ÉÒÔΪnull£¬Ò²¿ÉÒÔΪ""£»Îªnull»ò""ʱָÏò¡°ÎҵĵçÄÔ¡±
        // return£ºÈç¹û°´ÏÂÁË¡°È·¶¨¡±°´Å¥ÔòΪÓû§Ñ¡ÔñµÄĿ¼£¬·ñÔò·µ»Ønull
        public static string ShowDialog( IntPtr hwndOwner, string Title, string lpszInitPath )
        {
            string folderName = null;
            BROWSEINFO BrInfo;
            BrInfo.hwndOwner = hwndOwner;
            BrInfo.pidlRoot = IntPtr.Zero;
            BrInfo.pszDisplayName = null;
            BrInfo.lpszTitle = Title;
            BrInfo.ulFlags = 0x0001; // BIF_RETURNONLYFSDIRS
            BrInfo.lpfn = new BrowseCallBackProc( BrowseCtrlCallback );
            BrInfo.lParam = lpszInitPath;
            BrInfo.iImage = 0;        IntPtr pidlDestination = SHBrowseForFolder( ref BrInfo );
            
            if( pidlDestination != IntPtr.Zero )
            {
                StringBuilder tmp = new StringBuilder( 4000 ); // MAX_PATH
                SHGetPathFromIDList( pidlDestination, tmp );
                folderName = tmp.ToString();
            }
            return folderName;
        }
    }
    #endregion
      

  2.   

    上面的中文注释显示不正确,重发一次:using System.Runtime.InteropServices;
    using System.Text;#region BrowseFolderClass
    public class BrowseFolderClass
    {
        /*
        private struct SHITEMID
        {
         ushort cb;
         byte adID; // BYTE abID[1];
        }
        private struct ITEMIDLIST
        {
         SHITEMID mkid;
        }
        */
        [StructLayout(LayoutKind.Sequential)]
            private struct BROWSEINFO
        {
            public IntPtr hwndOwner; // HWND hwndOwner
            public IntPtr pidlRoot; // const ITEMIDLIST* pidlRoot
            [MarshalAs(UnmanagedType.LPTStr)]
            public string pszDisplayName;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszTitle;
            public int ulFlags;
            [MarshalAs(UnmanagedType.FunctionPtr)]
            public BrowseCallBackProc lpfn;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lParam;
            public int iImage;
        }    [DllImport("User32.Dll")]
        [return : MarshalAs(UnmanagedType.Bool)]
        private extern static bool SendMessage( IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPTStr)] string lParam );
        [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
        private extern static IntPtr SHBrowseForFolder( ref BROWSEINFO lpbi );
        [DllImport("Shell32.dll", CharSet=CharSet.Auto)]
        [return : MarshalAs(UnmanagedType.Bool)]
        private extern static bool SHGetPathFromIDList( IntPtr pidl, StringBuilder pszPath );
        private delegate int BrowseCallBackProc( IntPtr hwnd, uint msg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData );
        private static int BrowseCtrlCallback( IntPtr hwnd, uint uMsg, IntPtr lParam, [MarshalAs(UnmanagedType.LPTStr)] string lpData )
        {
            if( uMsg == 1 ) // BFFM_INITIALIZED
            {
                string szPath = lpData;
                if( szPath!=null && szPath!="" )
                {
                    if( szPath[szPath.Length-1] != '\\' )
                        szPath += '\\';
                    SendMessage( hwnd, 0x400+103, 1, szPath ); // BFFM_SETSELECTION
                }
            }        return 0;    }    // hwndOwner:父窗体句柄
        // Title:标题;可以为null,也可以为""
        // lpszInitPath:初始目录;可以为null,也可以为"";为null或""时指向“我的电脑”
        // return:如果按下了“确定”按钮则为用户选择的目录,否则返回null
        public static string ShowDialog( IntPtr hwndOwner, string Title, string lpszInitPath )
        {
            string folderName = null;
            BROWSEINFO BrInfo;
            BrInfo.hwndOwner = hwndOwner;
            BrInfo.pidlRoot = IntPtr.Zero;
            BrInfo.pszDisplayName = null;
            BrInfo.lpszTitle = Title;
            BrInfo.ulFlags = 0x0001; // BIF_RETURNONLYFSDIRS
            BrInfo.lpfn = new BrowseCallBackProc( BrowseCtrlCallback );
            BrInfo.lParam = lpszInitPath;
            BrInfo.iImage = 0;        IntPtr pidlDestination = SHBrowseForFolder( ref BrInfo );
            
            if( pidlDestination != IntPtr.Zero )
            {
                StringBuilder tmp = new StringBuilder( 4000 ); // MAX_PATH
                SHGetPathFromIDList( pidlDestination, tmp );
                folderName = tmp.ToString();
            }
            return folderName;
        }
    }
    #endregion
      

  3.   

    这个可以用在.NET Compact Framework下面吗??
      

  4.   

    没有,自己做一个。在ce下也不应该把系统的文件夹显示给用户吧。
    用System.IO.Path和System.IO.DirectoryInfo获取路径信息然后递归获取子目录就行
      

  5.   

    在CE下,有些时候也是需要列出文件夹的,您说的方法,用代码来实现是可以的,不过我觉得在系统级别上,操作系统应该会提供这样的一个功能,比如,我最近发现用C++就可以实现,但是可恶的.NET.....居然没有。
    另外,我发现只要编写的程序带有类似ImageList这一类的东西,在Windows Mobile 2003第二版上运行就会出现问题,不知道是不是Visual Studio 2003的Bug,还请各位CF达人给在下一个比较圆满的答案,先谢谢了