Beispiel #1
0
        /// <summary>
        /// Function to create a file system watcher for the specified file.
        /// </summary>
        /// <param name="values">Values used to determine if there are any changes to the file.</param>
        /// <returns>The file system watcher used to determine if the file changes or not.</returns>
        private FileSystemWatcher CreateWatcher(WatchValues values)
        {
            FileSystemWatcher result;

            result = new FileSystemWatcher
            {
                NotifyFilter          = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.Size,
                Path                  = Path.GetDirectoryName(values.FilePath).FormatDirectory(Path.DirectorySeparatorChar),
                Filter                = "*" + Path.GetExtension(values.FilePath),
                IncludeSubdirectories = false,
                EnableRaisingEvents   = true
            };

            result.Changed += (s, e) =>
            {
                if (!string.Equals(values.FilePath, e.FullPath, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                values.HasChanges = true;
            };

            return(result);
        }
Beispiel #2
0
        /// <summary>Function to edit the image data in an external application.</summary>
        /// <param name="workingFile">The file containing the image data to edit.</param>
        /// <returns>
        ///   <b>true</b> if the image data was changed, <b>false</b> if not.</returns>
        /// <exception cref="IOException">Thrown if no associated executable was found for the file type.</exception>
        public bool EditImage(IGorgonVirtualFile workingFile)
        {
            string exePath = GetExecutable(workingFile);

            if ((string.IsNullOrWhiteSpace(exePath)) || (!File.Exists(exePath)))
            {
                throw new IOException(string.Format(Resources.GORIMG_ERR_NO_EXTERNAL_EDITOR, workingFile.Name));
            }

            GorgonApplication.MainForm.Visible = false;

            var startInfo = new ProcessStartInfo(exePath)
            {
                ErrorDialog             = true,
                ErrorDialogParentHandle = GorgonApplication.MainForm.Handle,
                RedirectStandardError   = true,
                RedirectStandardOutput  = true,
                WindowStyle             = ProcessWindowStyle.Maximized,
                UseShellExecute         = false,
                WorkingDirectory        = Path.GetDirectoryName(workingFile.PhysicalFile.FullPath).FormatDirectory(Path.DirectorySeparatorChar),
                Arguments = $"\"{workingFile.PhysicalFile.FullPath}\""
            };

            Process externalProcess = null;

            var values = new WatchValues()
            {
                FilePath   = workingFile.PhysicalFile.FullPath,
                HasChanges = false
            };

            FileSystemWatcher watcher = CreateWatcher(values);

            try
            {
                _log.Print($"Launching {startInfo.FileName} to edit {workingFile.FullPath}", LoggingLevel.Verbose);

                externalProcess = Process.Start(startInfo);
                externalProcess.WaitForExit();

                if (!values.HasChanges)
                {
                    _log.Print($"{startInfo.FileName} closed. No changes detected on {workingFile.FullPath}.", LoggingLevel.Verbose);
                    return(false);
                }

                _log.Print($"{startInfo.FileName} closed. Changes detected on {workingFile.FullPath}.", LoggingLevel.Verbose);
            }
            finally
            {
                watcher?.Dispose();
                externalProcess?.Dispose();
                GorgonApplication.MainForm.Visible = true;
            }

            return(true);
        }