using System; using System.IO; using System.Diagnostics; using System.Windows.Forms; using Microsoft.Win32; using System.Runtime.InteropServices; namespace Common { static class ExtTools { public static string CompareProgram; public static string CompareAttributes; static ExtTools() { // Autodetect location of Beyond Compare executable CompareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe"; if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe"; if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe"; if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe"; CompareAttributes = "\"<>\" \"<>\""; } public static string GetTempDir() { return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + Application.ProductName + "\\Temp"; } public static void CompareText(string textLeft, string textRight, string fileNameLeft = "FileLeft.txt", string fileNameRight = "FileRight.txt") { if (!Directory.Exists(GetTempDir())) { Directory.CreateDirectory(GetTempDir()); } string tempFileLeft = GetTempDir() + "\\" + fileNameLeft; string tempFileRight = GetTempDir() + "\\" + fileNameRight; FileExt.WriteAllTextUpdate(tempFileLeft, textLeft); FileExt.WriteAllTextUpdate(tempFileRight, textRight); CompareFiles(tempFileLeft, tempFileRight); } public static void CompareFiles(string filenameLeft, string filenameRight) { try { // Execute comparison process string attributes = CompareAttributes; attributes = attributes.Replace("<>", filenameLeft); attributes = attributes.Replace("<>", filenameRight); ExecuteProgram(CompareProgram, attributes); } catch (Exception e) { MessageBox.Show(e.Message); } } public static void ExecuteProgram(string executableFileName, string arguments = "", string workingDirectory = "") { // Execute external process Process process = new Process(); process.StartInfo.FileName = executableFileName; process.StartInfo.Arguments = arguments; if (!File.Exists(process.StartInfo.FileName)) throw new Exception("Executable file \"" + process.StartInfo.FileName + "\" not found."); process.EnableRaisingEvents = true; process.StartInfo.WorkingDirectory = workingDirectory; process.StartInfo.CreateNoWindow = false; process.StartInfo.UseShellExecute = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; process.Start(); } public static string ExecuteProgramWait(string executableFileName, string arguments, string dir = "") { string output = ""; // Execute external process Process process = new Process(); process.StartInfo.FileName = executableFileName; process.StartInfo.Arguments = arguments; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; try { process.EnableRaisingEvents = true; process.StartInfo.WorkingDirectory = dir; process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; process.Start(); output = process.StandardOutput.ReadToEnd(); output += process.StandardError.ReadToEnd(); process.WaitForExit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return output; } public static void SetFileAssociation(string extension, string progId, string openWith, string fileDescription) { RegistryKey BaseKey; RegistryKey OpenMethod; RegistryKey Shell; RegistryKey CurrentUser; BaseKey = Registry.ClassesRoot.CreateSubKey(extension); BaseKey?.SetValue("", progId); OpenMethod = Registry.ClassesRoot.CreateSubKey(progId); OpenMethod?.SetValue("", fileDescription); OpenMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0"); Shell = OpenMethod?.CreateSubKey("Shell"); Shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); Shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\""); Shell?.Close(); OpenMethod?.Close(); BaseKey?.Close(); // Delete explorer override CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true); CurrentUser?.DeleteSubKey("UserChoice", false); CurrentUser?.Close(); // Tell explorer the file association has been changed SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); } [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); } }