如何在 .NET Compact Framework 中实现“选择文件夹”标准对话框 是不是和在Windos 系统中一样,从shell32.dll里面扩展出来?还是有另外的办法??各位大侠帮忙啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是调用API实现的“选择文件夹”:using System.Runtime.InteropServices;using System.Text;#region BrowseFolderClasspublic 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 上面的中文注释显示不正确,重发一次:using System.Runtime.InteropServices;using System.Text;#region BrowseFolderClasspublic 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 这个可以用在.NET Compact Framework下面吗?? 没有,自己做一个。在ce下也不应该把系统的文件夹显示给用户吧。用System.IO.Path和System.IO.DirectoryInfo获取路径信息然后递归获取子目录就行 在CE下,有些时候也是需要列出文件夹的,您说的方法,用代码来实现是可以的,不过我觉得在系统级别上,操作系统应该会提供这样的一个功能,比如,我最近发现用C++就可以实现,但是可恶的.NET.....居然没有。另外,我发现只要编写的程序带有类似ImageList这一类的东西,在Windows Mobile 2003第二版上运行就会出现问题,不知道是不是Visual Studio 2003的Bug,还请各位CF达人给在下一个比较圆满的答案,先谢谢了 求高手啊。。 求正则 求助:异常错误,如何修改? C#和JAVA面向对象思想的片面比较 如何获取webBrowser控件中的图片? 关于enum 的问题 获得命令行参数简单问题 哎,朋友让我帮他看程序,自己又不懂C#,只能跑这儿来问问各位高手了:关于程序客户区位置不对的问题 C#读串口扫描枪的程序 Visual Studio 2012 SVN 源码管理 请教:用C#怎样连接SQL SERVER 2000 Analysis Service 的OLAP数据仓库 判断是否为图像文件
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
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
用System.IO.Path和System.IO.DirectoryInfo获取路径信息然后递归获取子目录就行
另外,我发现只要编写的程序带有类似ImageList这一类的东西,在Windows Mobile 2003第二版上运行就会出现问题,不知道是不是Visual Studio 2003的Bug,还请各位CF达人给在下一个比较圆满的答案,先谢谢了