/// <summary> /// 打开文件选择器 /// </summary> /// <param name="defaultPath">文件的完整路径</param> /// <param name="fileFilter"></param> /// <param name="title">Title</param> /// <returns></returns> public static string OpenFilePicker(string fileFilter, string defaultPath = "", string title = "选择文件") { string result = string.Empty; OpenFileName ofn = new OpenFileName(); ofn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(ofn); ofn.filter = fileFilter; // "PPT Files(*.pptx;*.3dpx)\0*.pptx;*.3dpx"; char[] maxPathChar = new char[256]; //char[] pathChar = defaultPath.ToCharArray(); //for (int i = 0; i < pathChar.Length; i++) //{ // if (i < maxPathChar.Length) // { // maxPathChar[i] = pathChar[i]; // } //} ofn.file = new string(maxPathChar); ofn.maxFile = ofn.file.Length; ofn.fileTitle = new string(new char[64]); ofn.maxFileTitle = ofn.fileTitle.Length; ofn.initialDir = string.IsNullOrEmpty(defaultPath) ? Application.dataPath : defaultPath; //默认路径 ofn.title = title; //ofn.defExt = "PPTX";//显示文件的类型 //注意 一下项目不一定要全选 但是0x00000008项不要缺少 ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 /*| 0x00000200*/ | 0x00000008; //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR //浏览|文件必须存在|路径必须存在|允许选择多个文件|不改变当前路径 if (DllOpenFileDialog.GetOpenFileName(ofn)) { result = ofn.file; } return(result); }
public static string GetFileDir(string varTitle, string varpath) { OpenDialogFile openFile = new OpenDialogFile(); openFile.structSize = Marshal.SizeOf(openFile); //窗口标题 if (string.IsNullOrEmpty(varTitle)) { varTitle = "Open Project"; } openFile.title = varTitle; varpath = varpath.Replace('/', '\\'); //默认路径 openFile.initialDir = varpath; openFile.file = new string(new char[256]); openFile.maxFile = openFile.file.Length; openFile.fileTitle = new string(new char[256]); openFile.maxFileTitle = openFile.fileTitle.Length; //OpenDialogDir ofn2 = new OpenDialogDir(); //ofn2.pszDisplayName = new string(new char[2000]); ; // 存放目录路径缓冲区 //if (string.IsNullOrEmpty(varTitle)) // ofn2.lpszTitle = "Open Project";// 标题 ////ofn2.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; // 新的样式,带编辑框 //IntPtr pidlPtr = DllOpenFileDialog.SHBrowseForFolder(ofn2); //char[] charArray = new char[2000]; //for (int i = 0; i < 2000; i++) // charArray[i] = '\0'; //DllOpenFileDialog.SHGetPathFromIDList(pidlPtr, charArray); //string fullDirPath = new String(charArray); //fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0')); //注意 一下项目不一定要全选 但是0x00000008项不要缺少 openFile.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; if (DllOpenFileDialog.GetOpenFileName(openFile)) { return(openFile.file); } return(null); }
public static string OpenFile(string type = "All files(*.*)|*.*", string title = "选择") { DialogFile pth = new DialogFile(); pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth); pth.filter = type;// "All files(*.*)|*.*"; pth.file = new string(new char[256]); pth.maxFile = pth.file.Length; pth.fileTitle = new string(new char[64]); pth.maxFileTitle = pth.fileTitle.Length; pth.initialDir = Application.dataPath; // default path pth.title = title; pth.defExt = "*.*"; pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; if (DllOpenFileDialog.GetOpenFileName(pth)) { string filepath = pth.file;//选择的文件路径; return(filepath); } return(null); }
public static void OpenSingleFile(out string filepath, out string filename, params string[] exts) { bool flag = false; filepath = ""; filename = ""; OpenDialogFile ofn = new OpenDialogFile(); ofn.structSize = Marshal.SizeOf(ofn); //var str = string.Format("flie {0}(*.{1})|*.{2}", exts[0], exts[0], exts[0]); //if(exts.Length > 1) //{ // for(int i=1;i<exts.Length;++i) // { // str = string.Format("{0}|flie {1}(*.{2})|*.{3}", str, exts[i], exts[i], exts[i]); // } //} var str = "files(*." + exts[0]; if (exts.Length > 1) { for (int i = 1; i < exts.Length; ++i) { str = string.Format("{0};*.{1}", str, exts[i]); } } str = string.Format("{0})\0*.{1}", str, exts[0]); if (exts.Length > 1) { for (int i = 1; i < exts.Length; ++i) { str = string.Format("{0};*.{1}", str, exts[i]); } } //str += "\0All files (*.*)\0*.*"; ofn.filter = str; ofn.filterIndex = 1; ofn.file = new string(new char[256]); ofn.maxFile = ofn.file.Length; ofn.fileTitle = new string(new char[64]); ofn.maxFileTitle = ofn.fileTitle.Length; ofn.initialDir = "c:\\"; //默认路径 //ofn.defExt = null; //注意 一下项目不一定要全选 但是0x00000008项不要缺少 ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; //OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR if (DllOpenFileDialog.GetOpenFileName(ofn)) { //StartCoroutine(WaitLoad(ofn.file));//加载图片到panle Debug.Log("Selected file with full path: {0}" + ofn.file); flag = true; } if (flag == true) { //System.Diagnostics.Process.Start(ofn.file); Debug.Log(ofn.file); filepath = ofn.file; Debug.Log(ofn.fileTitle); if (ofn.fileTitle != null) { string[] names = ofn.fileTitle.Split('.'); if (names.Length > 0) { filename = names[0]; } } } }