public static string OpenFileDialog(string title, params string[] extensions) { FileDialogConfig ofn = new FileDialogConfig(); ofn.structSize = Marshal.SizeOf(ofn); var filters = new List <string>(); filters.Add("All Files (*.*)"); filters.Add("*.*"); foreach (var ext in extensions) { filters.Add(ext); } ofn.filter = Filter(filters.ToArray()); ofn.filterIndex = 2; 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 = Application.dataPath; ofn.defExt = extensions[1]; ofn.title = title; ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008; // OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_NOCHANGEDIR ofn.dlgOwner = GetForegroundWindow(); // 这一步将文件选择窗口置顶。 if (!GetOpenFileName(ofn)) { return(null); } return(ofn.file); }
public static string SaveFileDialog(string title, params string[] extensions) { FileDialogConfig ofn = new FileDialogConfig(); ofn.structSize = Marshal.SizeOf(ofn); var filters = new List <string>(); foreach (var ext in extensions) { filters.Add(ext); } ofn.filter = Filter(filters.ToArray()); ofn.filterIndex = 2; 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 = Application.dataPath; ofn.title = title; ofn.flags = 0x00000002 | 0x00000004; // OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY ofn.dlgOwner = GetForegroundWindow(); // 这一步将文件选择窗口置顶。 if (!GetSaveFileName(ofn)) { return(null); } return(ofn.file); }
public static extern bool GetSaveFileName([In, Out] FileDialogConfig dialog);