/// <summary> /// 打开“选择目录”对话框,返回用户选择的路径 /// </summary> /// <returns>成功则返回路径、失败则返回""</returns> public static string GetSavePath(string tille = "choose", string defPath = "") { BrowseInfo browseInfo = new BrowseInfo(); browseInfo.displayName = new string(new char[256]); browseInfo.title = tille; browseInfo.flags = (int)BrowseFlag.BIF_RETURNONLYFSDIRS + (int)BrowseFlag.BIF_STATUSTEXT + (int)BrowseFlag.BIF_VALIDATE; //browseInfo.flags = 0x00000010 | 0x00000002 | 0x00000001 | 0x00000040 | 0x00000080; //需查阅MSDN,根据需求添加 IntPtr a = LocalDialog.SHBrowseForFolder(browseInfo); //打开对话框,让用户选择 if (a != IntPtr.Zero) { byte[] path = new byte[256]; LocalDialog.SHGetPathFromIDList(a, path); //调用系统函数,解析结果并输出至path中 //将路径的中文乱码恢复正常(稍后再说) Encoding utf8 = Encoding.UTF8; byte[] utf8Bytes = Encoding.Convert(Encoding.Default, utf8, path); string utf8String = utf8.GetString(utf8Bytes); utf8String = utf8String.Replace("\0", ""); return(utf8String); } return(""); }
public static extern IntPtr SHBrowseForFolder([In, Out] BrowseInfo browse);