Example #1
0
 private void btnClose_Click(object sender, EventArgs e)
 {
     DebugHelper.WriteLine("ShareX closing. Reason: Unhandled exception.");
     Application.Exit();
 }
Example #2
0
        private static T LoadInternal(string filePath, string backupFolder = null)
        {
            string typeName = typeof(T).Name;

            if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
            {
                DebugHelper.WriteLine($"{typeName} load started: {filePath}");

                try
                {
                    using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        if (fileStream.Length > 0)
                        {
                            T settings;

                            using (StreamReader streamReader = new StreamReader(fileStream))
                                using (JsonTextReader jsonReader = new JsonTextReader(streamReader))
                                {
                                    JsonSerializer serializer = new JsonSerializer();
                                    serializer.Converters.Add(new StringEnumConverter());
                                    serializer.DateTimeZoneHandling   = DateTimeZoneHandling.Local;
                                    serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
                                    serializer.Error += Serializer_Error;
                                    settings          = serializer.Deserialize <T>(jsonReader);
                                }

                            if (settings == null)
                            {
                                throw new Exception($"{typeName} object is null.");
                            }

                            DebugHelper.WriteLine($"{typeName} load finished: {filePath}");

                            return(settings);
                        }
                        else
                        {
                            throw new Exception($"{typeName} file stream length is 0.");
                        }
                    }
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e, $"{typeName} load failed: {filePath}");
                }
            }
            else
            {
                DebugHelper.WriteLine($"{typeName} file does not exist: {filePath}");
            }

            if (!string.IsNullOrEmpty(backupFolder))
            {
                string fileName       = Path.GetFileName(filePath);
                string backupFilePath = Path.Combine(backupFolder, fileName);
                return(LoadInternal(backupFilePath));
            }

            DebugHelper.WriteLine($"Loading new {typeName} instance.");

            return(new T());
        }
Example #3
0
 private void btnContinue_Click(object sender, EventArgs e)
 {
     DebugHelper.WriteLine("ShareX continue.");
     Close();
 }
Example #4
0
        private bool SaveInternal(string filePath)
        {
            string typeName = GetType().Name;

            DebugHelper.WriteLine($"{typeName} save started: {filePath}");

            bool isSuccess = false;

            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    lock (this)
                    {
                        Helpers.CreateDirectoryFromFilePath(filePath);

                        string tempFilePath = filePath + ".temp";

                        using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                            using (StreamWriter streamWriter = new StreamWriter(fileStream))
                                using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                                {
                                    JsonSerializer serializer = new JsonSerializer();
                                    serializer.ContractResolver = new WritablePropertiesOnlyResolver();
                                    serializer.Converters.Add(new StringEnumConverter());
                                    serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                                    serializer.Formatting           = Formatting.Indented;
                                    serializer.Serialize(jsonWriter, this);
                                    jsonWriter.Flush();
                                }

                        if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
                        {
                            throw new Exception($"{typeName} file is corrupt: {tempFilePath}");
                        }

                        string backupFilePath = null;

                        if (CreateBackup)
                        {
                            string fileName = Path.GetFileName(filePath);
                            backupFilePath = Path.Combine(BackupFolder, fileName);
                            Helpers.CreateDirectoryFromDirectoryPath(BackupFolder);
                        }

                        File.Replace(tempFilePath, filePath, backupFilePath);

                        if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
                        {
                            Helpers.BackupFileWeekly(filePath, BackupFolder);
                        }

                        isSuccess = true;
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);

                OnSettingsSaveFailed(e);
            }
            finally
            {
                string status = isSuccess ? "successful" : "failed";
                DebugHelper.WriteLine($"{typeName} save {status}: {filePath}");
            }

            return(isSuccess);
        }
Example #5
0
        private bool SaveInternal(string filePath)
        {
            string typeName = GetType().Name;

            DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath);

            bool isSuccess = false;

            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    lock (this)
                    {
                        Helpers.CreateDirectoryFromFilePath(filePath);

                        string tempFilePath = filePath + ".temp";

                        using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                            using (StreamWriter streamWriter = new StreamWriter(fileStream))
                                using (JsonTextWriter jsonWriter = new JsonTextWriter(streamWriter))
                                {
                                    jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                                    jsonWriter.Formatting           = Formatting.Indented;

                                    JsonSerializer serializer = new JsonSerializer();
                                    serializer.ContractResolver = new WritablePropertiesOnlyResolver();
                                    serializer.Converters.Add(new StringEnumConverter());
                                    serializer.Serialize(jsonWriter, this);
                                    jsonWriter.Flush();
                                }

                        if (File.Exists(filePath))
                        {
                            if (CreateBackup)
                            {
                                Helpers.CopyFile(filePath, BackupFolder);
                            }

                            File.Delete(filePath);
                        }

                        File.Move(tempFilePath, filePath);

                        if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
                        {
                            Helpers.BackupFileWeekly(filePath, BackupFolder);
                        }

                        isSuccess = true;
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);
            }
            finally
            {
                DebugHelper.WriteLine("{0} save {1}: {2}", typeName, isSuccess ? "successful" : "failed", filePath);
            }

            return(isSuccess);
        }
Example #6
0
        public string Run(string inputPath)
        {
            pendingInputFilePath = null;
            string path = GetFullPath();

            if (!string.IsNullOrEmpty(path) && File.Exists(path) && !string.IsNullOrWhiteSpace(inputPath))
            {
                inputPath = inputPath.Trim('"');

                if (CheckExtension(inputPath))
                {
                    try
                    {
                        string outputPath = inputPath;

                        string arguments;

                        if (string.IsNullOrEmpty(Args))
                        {
                            arguments = '"' + inputPath + '"';
                        }
                        else
                        {
                            if (!string.IsNullOrWhiteSpace(OutputExtension))
                            {
                                outputPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(inputPath), System.IO.Path.GetFileNameWithoutExtension(inputPath));

                                if (!OutputExtension.StartsWith("."))
                                {
                                    outputPath += ".";
                                }

                                outputPath += OutputExtension;
                            }

                            arguments = CodeMenuEntryActions.Parse(Args, inputPath, outputPath);
                        }

                        using (Process process = new Process())
                        {
                            ProcessStartInfo psi = new ProcessStartInfo()
                            {
                                FileName        = path,
                                Arguments       = arguments,
                                UseShellExecute = false,
                                CreateNoWindow  = HiddenWindow
                            };

                            DebugHelper.WriteLine($"Action input: \"{inputPath}\" [{FileHelpers.GetFileSizeReadable(inputPath)}]");
                            DebugHelper.WriteLine($"Action run: \"{psi.FileName}\" {psi.Arguments}");

                            process.StartInfo = psi;
                            process.Start();
                            process.WaitForExit();
                        }

                        if (!string.IsNullOrEmpty(outputPath) && File.Exists(outputPath))
                        {
                            DebugHelper.WriteLine($"Action output: \"{outputPath}\" [{FileHelpers.GetFileSizeReadable(outputPath)}]");

                            if (DeleteInputFile && !inputPath.Equals(outputPath, StringComparison.OrdinalIgnoreCase))
                            {
                                pendingInputFilePath = inputPath;
                            }

                            return(outputPath);
                        }

                        return(inputPath);
                    }
                    catch (Exception e)
                    {
                        DebugHelper.WriteException(e);
                    }
                }
            }

            return(null);
        }
Example #7
0
        private bool SaveInternal(string filePath)
        {
            string typeName = GetType().Name;

            DebugHelper.WriteLine($"{typeName} save started: {filePath}");

            bool isSuccess = false;

            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    lock (this)
                    {
                        FileHelpers.CreateDirectoryFromFilePath(filePath);

                        string tempFilePath = filePath + ".temp";

                        using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough))
                        {
                            SaveToStream(fileStream, SupportDPAPIEncryption);
                        }

                        if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
                        {
                            throw new Exception($"{typeName} file is corrupt: {tempFilePath}");
                        }

                        if (File.Exists(filePath))
                        {
                            string backupFilePath = null;

                            if (CreateBackup)
                            {
                                string fileName = Path.GetFileName(filePath);
                                backupFilePath = Path.Combine(BackupFolder, fileName);
                                FileHelpers.CreateDirectory(BackupFolder);
                            }

                            File.Replace(tempFilePath, filePath, backupFilePath);
                        }
                        else
                        {
                            File.Move(tempFilePath, filePath);
                        }

                        if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
                        {
                            FileHelpers.BackupFileWeekly(filePath, BackupFolder);
                        }

                        isSuccess = true;
                    }
                }
            }
            catch (Exception e)
            {
                DebugHelper.WriteException(e);

                OnSettingsSaveFailed(e);
            }
            finally
            {
                string status = isSuccess ? "successful" : "failed";
                DebugHelper.WriteLine($"{typeName} save {status}: {filePath}");
            }

            return(isSuccess);
        }
Example #8
0
        public string Run(string filePath)
        {
            if (!string.IsNullOrEmpty(filePath) && CheckExtensions(filePath) && !string.IsNullOrEmpty(Path) && File.Exists(Path))
            {
                filePath = filePath.Trim('"');

                try
                {
                    string newFilePath = "";

                    using (Process process = new Process())
                    {
                        ProcessStartInfo psi = new ProcessStartInfo(Path);

                        if (string.IsNullOrEmpty(Args))
                        {
                            psi.Arguments = '"' + filePath + '"';
                        }
                        else
                        {
                            string args = Args.Replace("%filepath%", '"' + filePath + '"').Replace("%input", '"' + filePath + '"');

                            if (!string.IsNullOrEmpty(OutputExtension))
                            {
                                newFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(filePath), System.IO.Path.GetFileNameWithoutExtension(filePath));

                                if (!OutputExtension.Contains("."))
                                {
                                    OutputExtension = "." + OutputExtension;
                                }

                                newFilePath += OutputExtension;
                                args         = args.Replace("%output", '"' + newFilePath + '"');
                            }

                            psi.Arguments = args;
                        }

                        process.StartInfo = psi;

                        DebugHelper.WriteLine(string.Format("Running {0} with arguments: {1}", Path, psi.Arguments));

                        process.Start();
                        process.WaitForExit();
                    }

                    if (!string.IsNullOrEmpty(newFilePath) && File.Exists(newFilePath))
                    {
                        return(newFilePath);
                    }

                    return(filePath);
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e);
                }
            }

            return(filePath);
        }