From e0b5d616f4682d0c2e52d14305d6e6088f1c7a61 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 24 Nov 2019 09:30:31 +0200 Subject: [PATCH] Some methods was moved to a separate file --- Crypto Notepad/AboutFrom.cs | 30 +---- Crypto Notepad/Crypto Notepad.csproj | 2 +- Crypto Notepad/MainForm.cs | 36 +----- Crypto Notepad/Methods.cs | 177 +++++++++++++++++++++++++++ Crypto Notepad/SettingsForm.cs | 93 +------------- Crypto Notepad/WinFormsExtensions.cs | 24 ---- 6 files changed, 188 insertions(+), 174 deletions(-) create mode 100644 Crypto Notepad/Methods.cs delete mode 100644 Crypto Notepad/WinFormsExtensions.cs diff --git a/Crypto Notepad/AboutFrom.cs b/Crypto Notepad/AboutFrom.cs index 93bdac5..a23a4e8 100644 --- a/Crypto Notepad/AboutFrom.cs +++ b/Crypto Notepad/AboutFrom.cs @@ -17,35 +17,9 @@ public AboutFrom() InitializeComponent(); } - #region Methods - private static string CheckFor45DotVersion(int releaseKey) + #region Methods + private void GetDebugInfo() { - 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"; - } - - private async void GetDebugInfo() - { - copyToClipboardLabel.Visible = true; Version vrs = new Version(Application.ProductVersion); StringBuilder sb = new StringBuilder(string.Empty); diff --git a/Crypto Notepad/Crypto Notepad.csproj b/Crypto Notepad/Crypto Notepad.csproj index 30801c0..f79d11a 100644 --- a/Crypto Notepad/Crypto Notepad.csproj +++ b/Crypto Notepad/Crypto Notepad.csproj @@ -158,7 +158,7 @@ Component - + AboutFrom.cs diff --git a/Crypto Notepad/MainForm.cs b/Crypto Notepad/MainForm.cs index 2930d28..efb44c1 100644 --- a/Crypto Notepad/MainForm.cs +++ b/Crypto Notepad/MainForm.cs @@ -23,8 +23,6 @@ public partial class MainForm : Form bool cancelPressed = false; int findPos = 0; int caretPos; - static readonly string[] SizeSuffixes = - { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; public MainForm() { InitializeComponent(); @@ -51,21 +49,6 @@ protected override void WndProc(ref Message m) } #region Methods - 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]); - } - private async Task DecryptAES() { EnterKeyForm enterKeyForm = new EnterKeyForm(); @@ -232,21 +215,6 @@ private async void ContextMenuEncryptReplace() richTextBox.Modified = false; } - private 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); - } - } - private void SaveConfirm() { string messageBoxText; @@ -429,7 +397,7 @@ protected internal void StatusPanelFileInfo() if (!string.IsNullOrEmpty(filePath)) { long length = new FileInfo(filePath).Length; - statusPanelSizeLabel.Text = "Size: " + SizeSuffix(length).ToString(); + statusPanelSizeLabel.Text = "Size: " + Methods.SizeSuffix(length).ToString(); StatusPanelTextInfo(); } } @@ -850,7 +818,7 @@ private async void MainWindow_Load(object sender, EventArgs e) { Visible = false; LoadSettings(); - DeleteUpdateFiles(); + Methods.DeleteUpdateFiles(); MenuIcons(settings.menuIcons); ToolbarIcons(settings.oldToolbarIcons); if (args.Length == 2) /*drag & drop to executable*/ diff --git a/Crypto Notepad/Methods.cs b/Crypto Notepad/Methods.cs new file mode 100644 index 0000000..cc52438 --- /dev/null +++ b/Crypto Notepad/Methods.cs @@ -0,0 +1,177 @@ +using Microsoft.Win32; +using System; +using System.IO; +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 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 applicationExecutablePath, 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("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(); + } + + + } +} diff --git a/Crypto Notepad/SettingsForm.cs b/Crypto Notepad/SettingsForm.cs index 048a1ed..7c83b5c 100644 --- a/Crypto Notepad/SettingsForm.cs +++ b/Crypto Notepad/SettingsForm.cs @@ -58,88 +58,7 @@ private void LoadSettings() statusPanelSizeCheckBox.Checked = settings.statusPanelSize; statusPanelLabelsGroupBox.Visible = settings.statusPanelVisible; encryptionHintLabel.Visible = settings.encryptionHint; - } - - private 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) - { - - } - } - - private static void DissociateExtension(string applicationExecutablePath, 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 { } - } - - private 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("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 { } - } - - private 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; - WshShell shell = new WshShell(); - IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation); - shortcut.Description = PublicVar.appName; - shortcut.IconLocation = targetFileLocation; - shortcut.TargetPath = targetFileLocation; - shortcut.Arguments = "/s"; - shortcut.Save(); - } + } #endregion @@ -282,11 +201,11 @@ private void AssociateCheckBox_Click(object sender, EventArgs e) { if (associateCheckBox.Checked) { - AssociateExtension(Assembly.GetEntryAssembly().Location, "cnp"); + Methods.AssociateExtension(Assembly.GetEntryAssembly().Location, "cnp"); } else { - DissociateExtension(Assembly.GetEntryAssembly().Location, "cnp"); + Methods.DissociateExtension(Assembly.GetEntryAssembly().Location, "cnp"); } settings.explorerAssociate = associateCheckBox.Checked; } @@ -295,11 +214,11 @@ private void IntegrateCheckBox_Click(object sender, EventArgs e) { if (integrateCheckBox.Checked) { - MenuIntegrate("enable"); + Methods.MenuIntegrate("enable"); } else { - MenuIntegrate("disable"); + Methods.MenuIntegrate("disable"); } settings.explorerIntegrate = integrateCheckBox.Checked; } @@ -322,7 +241,7 @@ private void SendToCheckBox_Click(object sender, EventArgs e) { if (sendToCheckBox.Checked) { - SendToShortcut(); + Methods.SendToShortcut(); } else { diff --git a/Crypto Notepad/WinFormsExtensions.cs b/Crypto Notepad/WinFormsExtensions.cs deleted file mode 100644 index 24441c9..0000000 --- a/Crypto Notepad/WinFormsExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Windows.Forms; - -namespace Crypto_Notepad -{ - public static class WinFormsExtensions - { - 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 void Times(this int count, Action action) - { - for (int i = 0; i < count; i++) - { - action(); - } - } - } -}