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);
                }
            }
        }
Exemplo n.º 2
0
        public string[] OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
        {
            OpenFileName ofn = new OpenFileName();

            ofn.structSize = Marshal.SizeOf(ofn);

            var    allExts = new List <string>();
            string filter  = "";

            for (int i = 0; i < extensions.Length; i++)
            {
                var exts = "";
                for (var j = 0; j < extensions[i].Extensions.Length; j++)
                {
                    exts += (j > 0 ? ";" : "") + "*." + (extensions[i].Extensions[j]);
                    allExts.Add(extensions[i].Extensions[j]);
                }
                filter += extensions[i].Name + " (" + exts + ")\0" + exts + "\0";
            }
            ofn.filter       = filter;
            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   = directory;
            ofn.title        = title;

            if (LibWrap.GetOpenFileName(ofn))
            {
                string fileName       = ofn.file;
                bool   foundExtension = false;
                foreach (var ext in allExts)
                {
                    if (fileName.EndsWith("." + ext))
                    {
                        foundExtension = true;
                        break;
                    }
                }
                if (!foundExtension)
                {
                    fileName = fileName + "." + allExts[0];
                }

                return(new string[] { fileName });
            }
            return(null);
        }
        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);
                }
            }
        }
Exemplo n.º 4
0
 public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);