public string SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions) { var ofn = new OpenFileName(); try { ofn.structSize = Marshal.SizeOf(ofn); ofn.dlgOwner = LibWrap.GetActiveWindow(); ofn.filter = GetWindowsFilterFromFileExtensionList(extensions); ofn.maxFile = 2048; ofn.file = Marshal.StringToHGlobalUni(new string(new char[ofn.maxFile])); ofn.maxFileTitle = 64; ofn.fileTitle = new string(new char[ofn.maxFileTitle]); ofn.initialDir = GetDirectoryPath(directory); ofn.title = title; // Alternatively could change after, but that means the overwrite prompt doesn't work if (extensions != null && extensions.Length > 0 && extensions[0].Extensions.Length > 0) { ofn.defExt = extensions[0].Extensions[0]; } ofn.flags = OpenFileName.OFN_NOCHANGEDIR | OpenFileName.OFN_EXPLORER | OpenFileName.OFN_PATHMUSTEXIST | OpenFileName.OFN_OVERWRITEPROMPT; if (LibWrap.GetSaveFileName(ofn)) { var filenames = GetFilenamesUni(ofn.file); if (filenames.Count == 1 && filenames[0] != null) { var rc = filenames[0]; //if (ofn.fileExtension == 0) //{ // if (extensions != null && // ofn.filterIndex > 0 && // ofn.filterIndex <= extensions.Length) // { // var extensionList = extensions[ofn.filterIndex - 1]; // if (extensionList.Extensions.Length > 0) // { // rc = Path.ChangeExtension(rc, "." + extensionList.Extensions[0]); // } // } //} return(rc); } } return(string.Empty); } finally { if (ofn.file != IntPtr.Zero) { Marshal.FreeHGlobal(ofn.file); } } }
public string[] OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect) { var ofn = new OpenFileName(); try { ofn.structSize = Marshal.SizeOf(ofn); ofn.dlgOwner = LibWrap.GetActiveWindow(); ofn.filter = GetWindowsFilterFromFileExtensionList(extensions); ofn.maxFile = 2048; ofn.file = Marshal.StringToHGlobalUni(new string(new char[ofn.maxFile])); ofn.maxFileTitle = 64; ofn.fileTitle = new string(new char[ofn.maxFileTitle]); ofn.initialDir = GetDirectoryPath(directory); ofn.title = title; ofn.flags = OpenFileName.OFN_NOCHANGEDIR | OpenFileName.OFN_EXPLORER | OpenFileName.OFN_PATHMUSTEXIST | OpenFileName.OFN_FILEMUSTEXIST; if (multiselect) { ofn.flags |= OpenFileName.OFN_ALLOWMULTISELECT; } if (LibWrap.GetOpenFileName(ofn)) { return(GetFilenamesUni(ofn.file).ToArray()); } return(new string[] { }); } finally { if (ofn.file != IntPtr.Zero) { Marshal.FreeHGlobal(ofn.file); } } }