コード例 #1
0
        private void CreateArchive()
        {
            // Create a new instance of our ProgressBar Delegate that points
            //  to the ProgressBar's SetValue method.
            var updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar.SetValue);
            var updatePtDelegate = new UpdateProgressTextDelegate(ProgressText.SetValue);

            // progress text
            Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, ModPlusAPI.Language.GetItem(LangItem, "msg16"));

            // create temp folder
            var tmpFolder = Path.Combine(_dwgBaseFolder, "Temp");

            if (!Directory.Exists(tmpFolder))
            {
                Directory.CreateDirectory(tmpFolder);
            }

            // create base file with selected files items
            ProgressBar.Minimum = 0;
            ProgressBar.Maximum = _dwgBaseItems.Count;
            ProgressBar.Value   = 0;

            // Соберем список файлов-источников чтобы проще их сверять
            var sourceFiles = new List <string>();

            foreach (var fileToBind in _filesToBind)
            {
                if (!sourceFiles.Contains(fileToBind.SourceFile))
                {
                    sourceFiles.Add(fileToBind.SourceFile);
                }
            }

            var baseFileToArchive = new List <DwgBaseItem>();

            for (var i = 0; i < _dwgBaseItems.Count; i++)
            {
                Dispatcher.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty,
                                  ModPlusAPI.Language.GetItem(LangItem, "msg17") + ": " + i + "/" + _dwgBaseItems.Count);
                Dispatcher.Invoke(updatePbDelegate, DispatcherPriority.Background, System.Windows.Controls.Primitives.RangeBase.ValueProperty, (double)i);
                var dwgBaseItem = _dwgBaseItems[i];
                if (sourceFiles.Contains(dwgBaseItem.SourceFile))
                {
                    if (!baseFileToArchive.Contains(dwgBaseItem))
                    {
                        baseFileToArchive.Add(dwgBaseItem);
                    }
                }
            }

            // save xml file
            var xmlToArchive = Path.Combine(tmpFolder, "UserDwgBase.xml");

            DwgBaseHelpers.SerializerToXml(baseFileToArchive, xmlToArchive);
            if (!File.Exists(xmlToArchive))
            {
                MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "msg18"), MessageBoxIcon.Close);
                return;
            }

            // comment file
            var commentFile = CreateCommentFile(tmpFolder);

            // create zip
            _currentFileToUpload = Path.ChangeExtension(Path.Combine(tmpFolder, Path.GetRandomFileName()), ".zip");
            if (File.Exists(_currentFileToUpload))
            {
                File.Delete(_currentFileToUpload);
            }

            using (var zip = ZipFile.Open(_currentFileToUpload, ZipArchiveMode.Create))
            {
                // create directories
                foreach (var fileToBind in _filesToBind.Where(x => x.Selected))
                {
                    zip.CreateEntryFromFile(
                        fileToBind.FullFileName,
                        Path.Combine(fileToBind.SubDirectory, fileToBind.FileName));
                }

                // add xml file and delete him
                zip.CreateEntryFromFile(xmlToArchive, new FileInfo(xmlToArchive).Name);

                // add comment file
                if (!string.IsNullOrEmpty(commentFile) && File.Exists(commentFile))
                {
                    zip.CreateEntryFromFile(commentFile, new FileInfo(commentFile).Name);
                }
            }

            File.Delete(xmlToArchive);

            // show buttons
            BtMakeArchive.Visibility   = Visibility.Collapsed;
            BtSeeArchive.Visibility    = Visibility.Visible;
            BtDeleteArchive.Visibility = Visibility.Visible;
            BtUploadArchive.Visibility = Visibility.Visible;
            Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, ModPlusAPI.Language.GetItem(LangItem, "msg19"));
        }
コード例 #2
0
        private void MultiChangePath_Accept_OnClick(object sender, RoutedEventArgs e)
        {
            if (MultiChangePath_Path.Text.Equals("Блоки/") | MultiChangePath_Path.Text.Equals("Чертежи/"))
            {
                ModPlusAPI.Windows.MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "u25"));
                MultiChangePath_Path.Focus();
                return;
            }

            var selectedItems = new List <DwgBaseItem>();

            foreach (DwgBaseItemWithSelector item in MultiChangePath_LvItems.Items)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Item);
                }
            }

            if (!selectedItems.Any())
            {
                ModPlusAPI.Windows.MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "u26"));
                return;
            }

            if (!ModPlusAPI.Windows.MessageBox.ShowYesNo(
                    ModPlusAPI.Language.GetItem(LangItem, "u27"),
                    MessageBoxIcon.Question))
            {
                return;
            }

            var updatePbDelegate = new UpdateProgressBarDelegate(ProgressBar.SetValue);
            var updatePtDelegate = new UpdateProgressTextDelegate(ProgressText.SetValue);

            ProgressBar.Minimum = 0;
            ProgressBar.Maximum = _dwgBaseItems.Count;
            ProgressBar.Value   = 0;
            var index = 1;

            foreach (var dwgBaseItem in _dwgBaseItems)
            {
                foreach (var selectedItem in selectedItems)
                {
                    if (dwgBaseItem.Equals(selectedItem))
                    {
                        dwgBaseItem.Path = MultiChangePath_Path.Text;
                    }
                }

                Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty,
                                   $"{ModPlusAPI.Language.GetItem(LangItem, "u24")}: {index}/{_dwgBaseItems.Count}");
                Dispatcher?.Invoke(updatePbDelegate, DispatcherPriority.Background,
                                   System.Windows.Controls.Primitives.RangeBase.ValueProperty, (double)index);
                index++;
            }

            // was changed
            UserBaseChanged = true;

            // save
            DwgBaseHelpers.SerializerToXml(_dwgBaseItems, _userDwgBaseFile);
            DwgBaseHelpers.DeserializeFromXml(_userDwgBaseFile, out _dwgBaseItems);
            Dispatcher?.Invoke(updatePtDelegate, DispatcherPriority.Background, TextBlock.TextProperty, ModPlusAPI.Language.GetItem(LangItem, "u28"));
            ModPlusAPI.Windows.MessageBox.Show(ModPlusAPI.Language.GetItem(LangItem, "u28"));

            ClearProgress();

            // refill
            MultiChangePath_FillData();
        }