예제 #1
0
        public static async Task Save <T>(T file, string?path = null)
            where T : FileBase, new()
        {
            try
            {
                FileInfoBase info = GetFileInfo <T>();

                if (path == null)
                {
                    bool useExplorerBrowser = SettingsService.Current.UseWindowsExplorer;

                    if (!useExplorerBrowser)
                    {
                        FileBrowserView browser = new FileBrowserView(info, FileBrowserView.Modes.Save);
                        await ViewService.ShowDrawer(browser);

                        while (browser.IsOpen)
                        {
                            await Task.Delay(10);
                        }

                        path = browser.FilePath;
                        useExplorerBrowser = browser.UseFileBrowser;
                    }

                    if (useExplorerBrowser)
                    {
                        path = await App.Current.Dispatcher.InvokeAsync <string?>(() =>
                        {
                            SaveFileDialog dlg = new SaveFileDialog();
                            dlg.Filter         = ToFilter(info);
                            bool?result        = dlg.ShowDialog();

                            if (result != true)
                            {
                                return(null);
                            }

                            string?dirName = Path.GetDirectoryName(dlg.FileName);

                            if (dirName == null)
                            {
                                throw new Exception("Failed to parse file name: " + dlg.FileName);
                            }

                            return(Path.Combine(dirName, Path.GetFileNameWithoutExtension(dlg.FileName)));
                        });
                    }

                    if (path == null)
                    {
                        return;
                    }
                }

                path += "." + info.Extension;

                using FileStream stream = new FileStream(path, FileMode.Create);
                info.SerializeFile(file, stream);
            }
            catch (Exception ex)
            {
                Log.Write(Severity.Error, new Exception("Failed to save file", ex));
            }
        }