source: Common/ExtTools.cs

Last change on this file was 13, checked in by chronos, 3 years ago
  • Modified: Fixes to existing classes.
  • Added: File type association method.
  • Added: RecentFiles and Table classes.
File size: 5.9 KB
Line 
1using System;
2using System.IO;
3using System.Diagnostics;
4using System.Windows.Forms;
5using Microsoft.Win32;
6using System.Runtime.InteropServices;
7
8namespace Common
9{
10 static class ExtTools
11 {
12 public static string CompareProgram;
13 public static string CompareAttributes;
14
15 static ExtTools()
16 {
17 // Autodetect location of Beyond Compare executable
18 CompareProgram = "C:\\Program Files\\Beyond Compare 4\\BCompare.exe";
19 if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 4\\BCompare.exe";
20 if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files\\Beyond Compare 3\\BCompare.exe";
21 if (!File.Exists(CompareProgram)) CompareProgram = "C:\\Program Files (x86)\\Beyond Compare 3\\BCompare.exe";
22
23 CompareAttributes = "\"<<left_file>>\" \"<<right_file>>\"";
24 }
25
26 public static string GetTempDir()
27 {
28 return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" +
29 Application.ProductName + "\\Temp";
30 }
31
32 public static void CompareText(string textLeft, string textRight, string fileNameLeft = "FileLeft.txt",
33 string fileNameRight = "FileRight.txt")
34 {
35 if (!Directory.Exists(GetTempDir()))
36 {
37 Directory.CreateDirectory(GetTempDir());
38 }
39 string tempFileLeft = GetTempDir() + "\\" + fileNameLeft;
40 string tempFileRight = GetTempDir() + "\\" + fileNameRight;
41 FileExt.WriteAllTextUpdate(tempFileLeft, textLeft);
42 FileExt.WriteAllTextUpdate(tempFileRight, textRight);
43 CompareFiles(tempFileLeft, tempFileRight);
44 }
45
46 public static void CompareFiles(string filenameLeft, string filenameRight)
47 {
48 try
49 {
50 // Execute comparison process
51 string attributes = CompareAttributes;
52 attributes = attributes.Replace("<<left_file>>", filenameLeft);
53 attributes = attributes.Replace("<<right_file>>", filenameRight);
54 ExecuteProgram(CompareProgram, attributes);
55 } catch (Exception e)
56 {
57 MessageBox.Show(e.Message);
58 }
59 }
60
61 public static void ExecuteProgram(string executableFileName, string arguments = "", string workingDirectory = "")
62 {
63 // Execute external process
64 Process process = new Process();
65 process.StartInfo.FileName = executableFileName;
66 process.StartInfo.Arguments = arguments;
67
68 if (!File.Exists(process.StartInfo.FileName))
69 throw new Exception("Executable file \"" + process.StartInfo.FileName + "\" not found.");
70
71 process.EnableRaisingEvents = true;
72 process.StartInfo.WorkingDirectory = workingDirectory;
73 process.StartInfo.CreateNoWindow = false;
74 process.StartInfo.UseShellExecute = true;
75 process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
76 process.Start();
77 }
78
79 public static string ExecuteProgramWait(string executableFileName, string arguments, string dir = "")
80 {
81 string output = "";
82 // Execute external process
83 Process process = new Process();
84 process.StartInfo.FileName = executableFileName;
85 process.StartInfo.Arguments = arguments;
86 process.StartInfo.UseShellExecute = false;
87 process.StartInfo.RedirectStandardOutput = true;
88 process.StartInfo.RedirectStandardError = true;
89
90 try
91 {
92 process.EnableRaisingEvents = true;
93 process.StartInfo.WorkingDirectory = dir;
94 process.StartInfo.CreateNoWindow = true;
95 process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
96 process.Start();
97 output = process.StandardOutput.ReadToEnd();
98 output += process.StandardError.ReadToEnd();
99 process.WaitForExit();
100 }
101 catch (Exception ex)
102 {
103 MessageBox.Show(ex.Message);
104 }
105 return output;
106 }
107
108 public static void SetFileAssociation(string extension, string progId, string openWith, string fileDescription)
109 {
110 RegistryKey BaseKey;
111 RegistryKey OpenMethod;
112 RegistryKey Shell;
113 RegistryKey CurrentUser;
114
115 BaseKey = Registry.ClassesRoot.CreateSubKey(extension);
116 BaseKey?.SetValue("", progId);
117
118 OpenMethod = Registry.ClassesRoot.CreateSubKey(progId);
119 OpenMethod?.SetValue("", fileDescription);
120 OpenMethod?.CreateSubKey("DefaultIcon")?.SetValue("", "\"" + openWith + "\",0");
121 Shell = OpenMethod?.CreateSubKey("Shell");
122 Shell?.CreateSubKey("edit")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
123 Shell?.CreateSubKey("open")?.CreateSubKey("command")?.SetValue("", "\"" + openWith + "\"" + " \"%1\"");
124 Shell?.Close();
125 OpenMethod?.Close();
126 BaseKey?.Close();
127
128 // Delete explorer override
129 CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true);
130 CurrentUser?.DeleteSubKey("UserChoice", false);
131 CurrentUser?.Close();
132
133 // Tell explorer the file association has been changed
134 SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
135 }
136
137 [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
138 public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
139 }
140}
Note: See TracBrowser for help on using the repository browser.