コード例 #1
0
        private void okButton_Click(object sender, EventArgs e)
        {
            if (importMode)
            {
                moveProfiles(importedProfiles, profiles);
            }
            else
            {
                // Choose output filename
                SaveFileDialog outputFilesChooser = new SaveFileDialog();
                outputFilesChooser.Title  = "Choose your output file";
                outputFilesChooser.Filter = "Zip archives|*.zip";
                if (outputFilesChooser.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string filename = outputFilesChooser.FileName;

                // This outputfile is accessed by moveProfiles, so must be initialised beforehand
                outputFile = new ZipOutputStream(File.OpenWrite(filename));

                ProfileManager to = new ProfileManager(path);
                moveProfiles(profiles, to);
                to.SaveProfiles();

                foreach (string file in FileUtil.AllFiles(path))
                {
                    ZipEntry newEntry = new ZipEntry(file.Substring(path.Length).TrimStart('\\', '/'));
                    outputFile.PutNextEntry(newEntry);
                    FileStream input = File.OpenRead(file);
                    FileUtil.copyData(input, outputFile);
                    input.Close();
                }
                outputFile.Close();
                MessageBox.Show("Completed successfully", "Export completed successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
#if UNUSED
            if (importMode)
            {
                // Gather the list of selected profiles and convert them to strings
                IList    list_selectedProfiles = profileListBox.SelectedItems;
                int      i = 0;
                string[] selectedProfiles = new string[list_selectedProfiles.Count];
                foreach (object o in list_selectedProfiles)
                {
                    selectedProfiles[i] = (string)o;
                    i++;
                }

                // List the profiles to be imported
                Profile[] profileList = importedProfiles.AllProfiles(selectedProfiles);
                // List the extra files to be copied
                List <string> fileList = importedProfiles.AllRequiredFiles(profileList);

                // Copy the files and store where they have been moved to (in the substitution table)
                #region copying
                Dictionary <string, string> substitutionTable = new Dictionary <string, string>();
                foreach (string file in fileList)
                {
                    string filename = Path.GetFileName(file);
                    string pathname = mainForm.MeGUIPath + "\\extra\\" + filename;
                    int    count    = 0;
                    while (File.Exists(pathname))
                    {
                        pathname = mainForm.MeGUIPath + "\\extra\\" + count + "_" + filename;
                        count++;
                    }
                    substitutionTable[file] = pathname;

                    /*                    if (substitutionTable.ContainsKey(file))
                     *                      substitutionTable.Remove(file);
                     *                  substitutionTable.Add(file, pathname);*/
                    try
                    {
                        if (!Directory.Exists(mainForm.MeGUIPath + "\\extra"))
                        {
                            Directory.CreateDirectory(mainForm.MeGUIPath + "\\extra");
                        }
                        Stream outputStream = File.OpenWrite(pathname);
                        copyData(inputFile.GetInputStream(extraFiles[file]), outputStream);
                        outputStream.Close();
                    }
                    catch (IOException ioe)
                    {
                        MessageBox.Show(
                            string.Format("Error opening selected file: {0}{1}{0}Import cancelled", Environment.NewLine, ioe.Message),
                            "Error opening file",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.Close();
                        return;
                    }
                }
                #endregion

                // Fix up the filenames with the substitution table
                ProfileManager.FixFileNames(profileList, substitutionTable);
                // Add the profiles to the main manager
                profiles.AddAll(profileList, mainForm.DialogManager);
                this.Close();
            }
            else // export mode
            {
                // Choose output filename
                SaveFileDialog outputFilesChooser = new SaveFileDialog();
                outputFilesChooser.Title  = "Choose your output file";
                outputFilesChooser.Filter = "Zip archives|*.zip";
                if (outputFilesChooser.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                string filename = outputFilesChooser.FileName;

                // List profile names
                IList    list_selectedProfiles = profileListBox.SelectedItems;
                int      i = 0;
                string[] selectedProfiles = new string[list_selectedProfiles.Count];
                foreach (object o in list_selectedProfiles)
                {
                    selectedProfiles[i] = (string)o;
                    i++;
                }
                // List profiles
                Profile[] profileList = profiles.AllProfiles(selectedProfiles);
                Dictionary <string, string> substitutionTable = new Dictionary <string, string>();
                Dictionary <string, string> fileList          = profiles.AllRequiredFiles(profileList, out substitutionTable);

                ProfileManager.FixFileNames(profileList, substitutionTable);

                ZipOutputStream outputFile = new ZipOutputStream(File.OpenWrite(filename));

                foreach (string zipFilename in fileList.Keys)
                {
                    ZipEntry newEntry = new ZipEntry(zipFilename);
                    outputFile.PutNextEntry(newEntry);
                    FileStream input = File.OpenRead(fileList[zipFilename]);
                    copyData(input, outputFile);
                    input.Close();
                }

                foreach (Profile prof in profileList)
                {
                    ZipEntry newEntry = new ZipEntry(ProfileManager.ProfilePath(prof, "").TrimStart(new char[] { '\\' }));
                    outputFile.PutNextEntry(newEntry);
                    XmlSerializer outputter = new XmlSerializer(prof.GetType());
                    outputter.Serialize(outputFile, prof);
                }
                outputFile.Close();
                MessageBox.Show("Completed successfully", "Export completed successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
#endif
        }