Esempio n. 1
0
        private async void StartBackup()
        {
            Log.Here().Important("Starting backup test...");

            debugWindowData.TotalBackupTimeText = $"Total Backup Time:";

            Stopwatch timer = new Stopwatch();

            timer.Start();

            var result = await BackupTest();

            timer.Stop();

            string elapsedTime = String.Format("{0:hh\\:mm\\:ss}", timer.Elapsed);

            debugWindowData.TotalBackupTimeText = $"Total Backup Time: {elapsedTime} | {result.ToString()}";

            debugWindowData.IsBackingUp     = false;
            debugWindowData.CanBackupFolder = FileCommands.IsValidDirectoryPath(debugWindowData.BackupFolderPath);
        }
        /*
         * private MainAppData Data { get; set; }
         *
         * public void SetData(MainAppData data)
         * {
         *      Data = data;
         * }
         */

        public void OpenDialogAndSave(Window ParentWindow, string Title, string FilePath, string FileContent, Action <bool, string> OnSave = null, string DefaultFileName = "", string InitialDirectory = "", params FileBrowserFilter[] Filters)
        {
            try
            {
                var fileDialog = new CommonSaveFileDialog();
                fileDialog.Title = Title;

                fileDialog.AlwaysAppendDefaultExtension = false;
                fileDialog.OverwritePrompt = true;

                //Log.Here().Important($"Initial directory: {InitialDirectory} | Default FileName: {DefaultFileName} | FilePath: {FilePath}");

                if (!String.IsNullOrEmpty(DefaultFileName))
                {
                    fileDialog.DefaultFileName = DefaultFileName;
                }

                //if (FileCommands.IsValidFilePath(InitialDirectory) && !FileCommands.IsValidDirectoryPath(InitialDirectory)) InitialDirectory = Directory.GetParent(InitialDirectory).FullName + @"\";

                if (!String.IsNullOrWhiteSpace(InitialDirectory) && FileCommands.IsValidDirectoryPath(InitialDirectory))
                {
                    fileDialog.InitialDirectory = Path.GetFullPath(InitialDirectory);
                }
                else
                {
                    if (FileCommands.IsValidPath(FilePath))
                    {
                        if (FileCommands.IsValidDirectoryPath(FilePath))
                        {
                            fileDialog.InitialDirectory = Path.GetFullPath(FilePath);
                        }
                        else
                        {
                            fileDialog.InitialDirectory = Directory.GetParent(FilePath).FullName;
                        }
                    }
                    else
                    {
                        fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
                    }
                }

                //Log.Here().Important($"Initial directory set to {fileDialog.InitialDirectory}");

                if (Filters != null)
                {
                    if (Filters.Length <= 0)
                    {
                        fileDialog.Filters.Add(new CommonFileDialogFilter(CommonFileFilters.All.Name, CommonFileFilters.All.Values));
                    }
                    else
                    {
                        foreach (var filter in Filters)
                        {
                            fileDialog.Filters.Add(new CommonFileDialogFilter(filter.Name, filter.Values));
                        }
                    }
                }
                else
                {
                    fileDialog.Filters.Add(new CommonFileDialogFilter(CommonFileFilters.All.Name, CommonFileFilters.All.Values));
                }

                var result = fileDialog.ShowDialog(ParentWindow);

                if (result == CommonFileDialogResult.Ok)
                {
                    bool success = FileCommands.WriteToFile(fileDialog.FileName, FileContent);
                    OnSave?.Invoke(success, fileDialog.FileName);
                }
            }
            catch (Exception ex)
            {
                Log.Here().Error($"Error opening dialog window: {ex.ToString()}");
            }
        }