Some methods was moved to a separate file

This commit is contained in:
Alexander 2019-11-24 09:30:31 +02:00
parent e671088df7
commit e0b5d616f4
6 changed files with 188 additions and 174 deletions

View file

@ -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);

View file

@ -158,7 +158,7 @@
<Compile Include="PlaceholderTextBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="WinFormsExtensions.cs" />
<Compile Include="Methods.cs" />
<EmbeddedResource Include="AboutFrom.resx">
<DependentUpon>AboutFrom.cs</DependentUpon>
</EmbeddedResource>

View file

@ -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*/

177
Crypto Notepad/Methods.cs Normal file
View file

@ -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();
}
}
}

View file

@ -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
{

View file

@ -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();
}
}
}
}