public override bool CanExecute(object parameter) { if (base.CanExecute(parameter) && parameter != null && FileCommands.Load != null) { string filePath = (String)parameter; return(FileCommands.IsValidPath(filePath)); } return(false); }
public void OnFileLocationChanged() { FileValidation = FileValidation.None; if (!String.IsNullOrWhiteSpace(FileLocationText)) { if (!FileCommands.IsValidPath(FileLocationText)) { FileValidation = FileValidation.Error; } else { if (BrowseType == FileBrowseType.Directory) { if (!FileCommands.DirectoryExists(FileLocationText)) { FileValidation = FileValidation.Warning; } } else if (BrowseType == FileBrowseType.File) { if (!FileCommands.FileExists(FileLocationText)) { FileValidation = FileValidation.Warning; } } } } if (FileValidation == FileValidation.None) { if (ToolTip != null) { ClearValue(FileBrowseControl.ToolTipProperty); } } else if (FileValidation == FileValidation.Warning) { if (BrowseType == FileBrowseType.Directory) { ToolTip = "Folder not found."; } else if (BrowseType == FileBrowseType.File) { ToolTip = "File not found."; } } else if (FileValidation == FileValidation.Error) { ToolTip = "Error: Path is not valid."; } }
public void LoadDataDirectory() { if (!FileCommands.IsValidPath(Data.Settings.DOS2DataDirectory)) { if (Data.Settings.FindDOS2DataDirectory()) { Log.Here().Warning("DOS2 data directory found via the registry."); saveModuleSettings = true; } else { Log.Here().Warning("DOS2 data directory not found."); } } else { Log.Here().Activity("DOS2 data directory set to {0}", Data.Settings.DOS2DataDirectory); } }
public void StartBrowse() { Window parentWindow = Window.GetWindow(this); //Log.Here().Activity($"LastFileLocation is {LastFileLocation} FileLocationText: {FileLocationText}"); if (!String.IsNullOrEmpty(FileLocationText) && FileCommands.IsValidPath(FileLocationText)) { if (BrowseType == FileBrowseType.File) { if (FileCommands.IsValidFilePath(FileLocationText)) { var parentDirectory = new DirectoryInfo(FileLocationText); if (parentDirectory != null) { LastFileLocation = parentDirectory.Parent.FullName; } } else { LastFileLocation = FileLocationText; } } else { var directory = new DirectoryInfo(FileLocationText); if (directory != null) { LastFileLocation = directory.FullName; } } } if (!FileCommands.IsValidPath(LastFileLocation)) { if (AppController.Main.CurrentModule != null && AppController.Main.CurrentModule.ModuleData != null) { LastFileLocation = DefaultPaths.ModuleRootFolder(AppController.Main.CurrentModule.ModuleData); } else { LastFileLocation = DefaultPaths.RootFolder; } // Path.GetFullPath(Assembly.GetExecutingAssembly().Location); } if (BrowseType == FileBrowseType.File) { CommonFileDialog fileDialog = null; if (BrowseMode == FileBrowserMode.Open) { var openFileDialog = new CommonOpenFileDialog(); openFileDialog.Multiselect = false; fileDialog = openFileDialog; } else if (BrowseMode == FileBrowserMode.Save) { var saveFileDialog = new CommonSaveFileDialog(); saveFileDialog.AlwaysAppendDefaultExtension = false; saveFileDialog.OverwritePrompt = true; fileDialog = saveFileDialog; } if (fileDialog != null) { fileDialog.Title = OpenFileText; fileDialog.InitialDirectory = LastFileLocation; string fileName = Path.GetFileName(LastFileLocation); //if (!String.IsNullOrWhiteSpace(DefaultExt)) fileDialog.DefaultExtension = DefaultExt; if (Filters != null) { if (Filters.Count <= 0) { Filters.Add(CommonFileFilters.All); } foreach (var filter in Filters) { fileDialog.Filters.Add(new CommonFileDialogFilter(filter.Name, filter.Values)); } } else { fileDialog.Filters.Add(new CommonFileDialogFilter("All Files", "*.*")); } if (FileCommands.IsValidFilePath(FileLocationText)) { fileDialog.DefaultFileName = Path.GetFileName(FileLocationText); } else if (!String.IsNullOrWhiteSpace(DefaultFileName)) { if (!String.IsNullOrWhiteSpace(DefaultExt)) { fileDialog.DefaultFileName = DefaultFileName + DefaultExt; } else { fileDialog.DefaultFileName = DefaultFileName; } } //Log.Here().Activity($"DefaultFileName: {fileDialog.DefaultFileName}"); var result = fileDialog.ShowDialog(parentWindow); if (result == CommonFileDialogResult.Ok) { string filename = fileDialog.FileName; if (fileDialog is CommonOpenFileDialog openFileDialog) { filename = String.Join(";", openFileDialog.FileNames); } else if (fileDialog is CommonSaveFileDialog saveFileDialog) { filename = saveFileDialog.FileName; } if (!FileCommands.IsValidFilePath(filename)) { filename = filename.CleanFileName(); filename = Path.GetFullPath(filename); } SkipNext = true; if (filename.Contains("%20")) { filename = Uri.UnescapeDataString(filename); // Get rid of %20 } filename = filename.Replace(DefaultPaths.AppFolder, ""); FileLocationText = filename; LastFileLocation = Path.GetDirectoryName(FileLocationText); OnOpen?.Execute(FileLocationText); } } } else if (BrowseType == FileBrowseType.Directory) { /* * VistaFolderBrowserDialog folderDialog = new VistaFolderBrowserDialog(); * folderDialog.SelectedPath = LastFileLocation; * folderDialog.Description = OpenFileText; * folderDialog.UseDescriptionForTitle = true; * folderDialog.ShowNewFolderButton = true; * * Nullable<bool> result = folderDialog.ShowDialog(parentWindow); */ CommonFileDialog fileDialog = null; if (BrowseMode == FileBrowserMode.Open) { var openFileDialog = new CommonOpenFileDialog(); openFileDialog.Multiselect = false; openFileDialog.AllowNonFileSystemItems = true; openFileDialog.IsFolderPicker = true; fileDialog = openFileDialog; } else if (BrowseMode == FileBrowserMode.Save) { fileDialog = new CommonSaveFileDialog(); } if (fileDialog != null) { fileDialog.Title = OpenFileText; fileDialog.InitialDirectory = LastFileLocation; fileDialog.DefaultDirectory = Path.GetFullPath(DefaultPaths.RootFolder); var result = fileDialog.ShowDialog(parentWindow); if (String.IsNullOrWhiteSpace(DefaultFileName)) { if (FileCommands.IsValidFilePath(FileLocationText)) { fileDialog.DefaultFileName = Path.GetDirectoryName(FileLocationText); } } else { fileDialog.DefaultFileName = DefaultFileName; } if (result == CommonFileDialogResult.Ok) { string path = ""; if (BrowseMode == FileBrowserMode.Open && fileDialog is CommonOpenFileDialog openFileDialog) { path = openFileDialog.FileNames.First(); } else { path = fileDialog.FileName; } if (path.Contains("%20")) { path = Uri.UnescapeDataString(path); // Get rid of %20 } path = path.Replace(DefaultPaths.AppFolder, ""); SkipNext = true; FileLocationText = path; LastFileLocation = FileLocationText; OnOpen?.Execute(FileLocationText); } } } AutoScrollTextbox(); }
public static bool SaveManagedProjects(DOS2DEModuleData Data) { Log.Here().Important("Saving Managed Projects data to {0}", Data.Settings.AddedProjectsFile); if (Data.ManagedProjectsData != null && Data.ManagedProjectsData.SavedProjects.Count > 0 && Data.Settings != null && FileCommands.IsValidPath(Data.Settings.AddedProjectsFile)) { //Data.ManagedProjectsData.Projects.Clear(); //Data.ManagedProjectsData.Projects.AddRange(Data.ManagedProjects.Select(m => m.ProjectAppData).ToList()); string json = JsonInterface.SerializeObject(Data.ManagedProjectsData); return(FileCommands.WriteToFile(Data.Settings.AddedProjectsFile, json)); } return(false); }
public void Init(IModuleData moduleData) { parentData = moduleData; /* * if (File.Exists(DefaultFilePath)) * { * DefaultEditorText = File.ReadAllText(DefaultFilePath); * } * else if(FileCommands.IsValidPath(DefaultFilePath) && !String.IsNullOrEmpty(DefaultEditorText)) * { * File.WriteAllText(DefaultFilePath, DefaultEditorText); * } */ if (String.IsNullOrWhiteSpace(FilePath)) { InitialDirectory = DefaultPaths.ModuleTemplatesFolder(parentData); FilePath = Path.Combine(InitialDirectory, DefaultFileName); } else { InitialDirectory = Directory.GetParent(FilePath).FullName + @"\"; } if (DefaultEditorText == null) { DefaultEditorText = ""; } if (String.IsNullOrEmpty(Name) && !String.IsNullOrEmpty(DefaultFileName)) { Name = DefaultFileName; } if (File.Exists(FilePath)) { EditorText = File.ReadAllText(FilePath); Log.Here().Important("Loaded {0} template file at {1}", Name, FilePath); } else { EditorText = DefaultEditorText; Log.Here().Warning("Template file {0} not found at {1}. Using default template.", Name, FilePath); } if (String.IsNullOrEmpty(OpenFileText)) { OpenFileText = "Select " + LabelText; } SaveAsText = "Save " + Name + " As..."; OpenCommand = new ParameterCommand((object param) => { if (param is string FileLocationText) { FilePath = FileLocationText; FileCommands.Save.SaveModuleSettings(parentData); EditorText = FileCommands.ReadFile(FilePath); } }); SaveCommand = new SaveFileCommand(OnSave, OnSaveAs); SaveAsCommand = new SaveFileAsCommand(OnSaveAs); if (!File.Exists(FilePath) && FileCommands.IsValidPath(FilePath) && !String.IsNullOrWhiteSpace(EditorText)) { FileCommands.WriteToFile(filePath, EditorText); } }
/* * 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()}"); } }