コード例 #1
0
        public static async Task <SaveResult> Save <T>(string?path = null)
            where T : FileBase
        {
            SaveResult result = default;

            try
            {
                result.Info = GetFileInfo <T>();

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

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

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

                        path = browser.FilePath;
                        useExplorerBrowser = browser.UseFileBrowser;
                        result.Options     = browser.OptionsControl;
                    }

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

                            if (dlgResult != 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(result);
                    }
                }

                path       += "." + result.Info.Extension;
                result.Path = path;

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

            return(result);
        }
コード例 #2
0
        public static async Task <OpenResult> Open(params FileInfoBase[] fileInfos)
        {
            OpenResult result = default;

            try
            {
                bool useExplorerBrowser = SettingsService.Current.UseWindowsExplorer;

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

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

                    result.Path        = browser.FilePath;
                    result.Options     = browser.OptionsControl;
                    useExplorerBrowser = browser.UseFileBrowser;
                }

                if (useExplorerBrowser)
                {
                    result.Path = await App.Current.Dispatcher.InvokeAsync <string?>(() =>
                    {
                        OpenFileDialog dlg = new OpenFileDialog();
                        dlg.Filter         = ToAnyFilter(fileInfos);
                        bool?result        = dlg.ShowDialog();

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

                        return(dlg.FileName);
                    });
                }

                if (result.Path == null)
                {
                    return(result);
                }

                string extension = Path.GetExtension(result.Path);
                result.Info = GetFileInfo(extension);

                using FileStream stream = new FileStream(result.Path, FileMode.Open);
                result.File             = result.Info.DeserializeFile(stream);

                if (result.File == null)
                {
                    throw new Exception("File failed to deserialize");
                }

                return(result);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to open file");
            }

            return(result);
        }
コード例 #3
0
 public EntryWrapper(IFileSource.IEntry entry, FileBrowserView view)
 {
     this.Entry = entry;
     this.View  = view;
 }
コード例 #4
0
        public static async Task Save <T>(Func <bool, Task <T?> > writeFile, string title, string?path = null)
            where T : FileBase, new()
        {
            try
            {
                bool advancedMode = false;

                FileInfoBase info = GetFileInfo <T>();

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

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

                        if (title != null)
                        {
                            title = "Save " + title;
                        }

                        await ViewService.ShowDrawer(browser, title);

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

                        path               = browser.FilePath;
                        advancedMode       = browser.AdvancedMode;
                        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;

                FileBase?file = await writeFile.Invoke(advancedMode);

                if (file == null)
                {
                    return;
                }

                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));
            }
        }
コード例 #5
0
        public async Task <FileBase> OpenAny(params FileType[] fileTypes)
        {
            try
            {
                string path         = null;
                bool   advancedMode = false;

                bool useExplorerBrowser = App.Settings.UseWindowsExplorer;

                if (!useExplorerBrowser)
                {
                    FileBrowserView browser = new FileBrowserView(this.fileSources, fileTypes, FileBrowserView.Modes.Load);
                    await App.Services.Get <IViewService>().ShowDrawer(browser);

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

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

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

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

                        return(dlg.FileName);
                    });

                    advancedMode = true;
                }

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

                string   extension = Path.GetExtension(path);
                FileType type      = GetFileType(fileTypes, extension);

                FileBase file = await Open(path, type);

                file.UseAdvancedLoad = advancedMode;
                return(file);
            }
            catch (Exception ex)
            {
                Log.Write(new Exception("Failed to open file", ex), "Files", Log.Severity.Error);
            }

            return(null);
        }
コード例 #6
0
        public async Task Save(Func <bool, Task <FileBase> > writeFile, FileType type, string path = null)
        {
            try
            {
                bool advancedMode = false;

                if (path == null)
                {
                    bool useExplorerBrowser = App.Settings.UseWindowsExplorer;

                    if (!useExplorerBrowser)
                    {
                        List <FileType> fileTypes = new List <FileType>();
                        fileTypes.Add(type);
                        FileBrowserView browser = new FileBrowserView(this.fileSources, fileTypes.ToArray(), FileBrowserView.Modes.Save);
                        await App.Services.Get <IViewService>().ShowDrawer(browser);

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

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

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

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

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

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

                path += "." + type.Extension;

                FileBase file = await writeFile.Invoke(advancedMode);

                using FileStream stream = new FileStream(path, FileMode.Create);
                if (type.Serialize != null)
                {
                    type.Serialize.Invoke(stream, file);
                }
                else
                {
                    using TextWriter writer = new StreamWriter(stream);
                    string json = serializer.Serialize(file);
                    writer.Write(json);
                }
            }
            catch (Exception ex)
            {
                Log.Write(new Exception("Failed to save file", ex), "Files", Log.Severity.Error);
            }
        }