using Microsoft.Win32; using System; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Windows.Forms; namespace Crypto_Notepad { public static class Methods { static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; public static void AppendLine(this TextBox source, string value) { if (source.Text.Length == 0) source.Text = value; else source.AppendText("\r\n" + value); } public static bool IsOnScreen(Form form) { Rectangle formRectangle = new Rectangle(form.Left, form.Top, form.Width, form.Height); return Screen.AllScreens.Any(s => s.WorkingArea.IntersectsWith(formRectangle)); } public static void Times(this int count, Action action) { for (int i = 0; i < count; i++) { action(); } } public static string CheckDotNetVersion(int releaseKey) { if (releaseKey >= 528040) return "4.8 or later"; if (releaseKey >= 461808) return "4.7.2"; if (releaseKey >= 461308) return "4.7.1"; if (releaseKey >= 460798) return "4.7"; if (releaseKey >= 394802) return "4.6.2"; if (releaseKey >= 394254) return "4.6.1"; if (releaseKey >= 393295) return "4.6"; if (releaseKey >= 379893) return "4.5.2"; if (releaseKey >= 378675) return "4.5.1"; if (releaseKey >= 378389) return "4.5"; return "No 4.5 or later version detected"; } public static string GetDotNetVersion() { int releaseKey; using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\")) { releaseKey = Convert.ToInt32(ndpKey.GetValue("Release")); } return CheckDotNetVersion(releaseKey); } public static void DeleteUpdateFiles() { string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\"; string UpdaterExe = exePath + "Updater.exe"; string UpdateZip = exePath + "Crypto-Notepad-Update.zip"; if (File.Exists(UpdaterExe)) { File.Delete(UpdaterExe); } if (File.Exists(UpdateZip)) { File.Delete(UpdateZip); } } public static string SizeSuffix(long value) { if (value < 0) { return "-" + SizeSuffix(-value); } int i = 0; decimal dValue = (decimal)value; while (Math.Round(dValue / 1024) >= 1) { dValue /= 1024; i++; } return string.Format("{0:n1} {1}", dValue, SizeSuffixes[i]); } public static void AssociateExtension(string applicationExecutablePath, string extension) { try { RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Classes", true); key.CreateSubKey("." + extension).SetValue(string.Empty, extension + "_auto_file"); key = key.CreateSubKey(extension + "_auto_file"); key.CreateSubKey("DefaultIcon").SetValue(string.Empty, applicationExecutablePath + ",0"); key = key.CreateSubKey("Shell"); key.SetValue(string.Empty, "Open"); key = key.CreateSubKey("Open"); key.CreateSubKey("Command").SetValue(string.Empty, "\"" + applicationExecutablePath + "\" \"%1\" /o"); key.CreateSubKey("ddeexec\\Topic").SetValue(string.Empty, "System"); } catch (Exception) { } } public static void DissociateExtension(string extension) { try { RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true); RegistryKey subKeyTree = Registry.CurrentUser.OpenSubKey(@"Software\Classes\" + extension + "_auto_file", true); if (subKeyTree != null) { key.DeleteSubKeyTree(extension + "_auto_file"); key.DeleteSubKeyTree("." + extension); } } catch { } } public static void MenuIntegrate(string action) { string appExePath = Assembly.GetEntryAssembly().Location; try { if (action == "enable") { RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\", true); key = key.CreateSubKey("*"); key = key.CreateSubKey("shell"); key.CreateSubKey("Crypto Notepad").SetValue("icon", appExePath); key.CreateSubKey("Crypto Notepad").SetValue("SubCommands", ""); key.CreateSubKey(@"Crypto Notepad\shell"); key.CreateSubKey(@"Crypto Notepad\shell\cmd1\").SetValue("MUIVerb", "Encrypt"); key.CreateSubKey(@"Crypto Notepad\shell\cmd1\command").SetValue(string.Empty, "\"" + appExePath + "\" \"%1\" /er"); key.CreateSubKey(@"Crypto Notepad\shell\cmd2\").SetValue("MUIVerb", "Decrypt"); key.CreateSubKey(@"Crypto Notepad\shell\cmd2\command").SetValue(string.Empty, "\"" + appExePath + "\" \"%1\" /o"); } if (action == "disable") { RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Classes\*\shell\", true); RegistryKey subKeyTree = Registry.CurrentUser.OpenSubKey(@"Software\Classes\*\shell\Crypto Notepad", true); if (subKeyTree != null) { key.DeleteSubKeyTree("Crypto Notepad"); } } } catch { } } public static void SendToShortcut() { string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\SendTo"; string shortcutName = PublicVar.appName + ".lnk"; string shortcutLocation = Path.Combine(shortcutPath, shortcutName); string targetFileLocation = Assembly.GetEntryAssembly().Location; IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutLocation); shortcut.Description = PublicVar.appName; shortcut.IconLocation = targetFileLocation; shortcut.TargetPath = targetFileLocation; shortcut.Arguments = "/s"; shortcut.Save(); } } }