예제 #1
0
        /// <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("");
        }
예제 #2
0
        public static OpenFileName OpenSaveFileWindow(string FormatName, int fileSize = 256)
        {
            OpenFileName openFileName = new OpenFileName();

            openFileName.structSize   = Marshal.SizeOf(openFileName);
            openFileName.filter       = $"{FormatName}文件(*.{FormatName})\0*.{FormatName}";
            openFileName.file         = new string(new char[fileSize]);
            openFileName.maxFile      = openFileName.file.Length;
            openFileName.fileTitle    = new string(new char[64]);
            openFileName.maxFileTitle = openFileName.fileTitle.Length;
            openFileName.initialDir   = Application.streamingAssetsPath.Replace('/', '\\');//默认路径
            openFileName.title        = "选择文件";
            openFileName.flags        = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

            if (LocalDialog.GetSaveFileName(openFileName))
            {
                UnityEngine.Debug.Log(openFileName.file);
                UnityEngine.Debug.Log(openFileName.fileTitle);
                return(openFileName);
            }
            return(null);
        }