Helpers Located in the HandyControl.Tools namespace.
1 | Using HandyControl.Tools; |
GlobalDataHelper
GlobalDataHelper only Available in .NetCore >= 3.0, so you cant use it in .Net Framework
with GlobalDataHelper you can save and read your application settings object to a json file
Example:
first of all you need to create a class and inherit from the GlobalDataHelper
, Then create your properties.
1 | public class AppConfig : GlobalDataHelper |
Note that there are 3 overriding properties:
By default, the Json file name is AppConfig.json
and will be placed next to the executable file, So if you want to change the name and location of the file, use the FileName property:
1 | public override string FileName { get; set; } = "setting.json"; |
If you have made major changes to your settings file and you get an error when running the program, you can use the File versioning, if you change the version number, the previous settings file will be deleted, and the new settings will be replaced.
1 | public override int FileVersion { get; set; } = 2; |
Read/Write Settings
for reading values
1 | var settings = GlobalDataHelper.Load<AppConfig>(); |
if you prefer async:
1 | var settings = await GlobalDataHelper.LoadAsync<AppConfig>(); |
for writing values
1 | var settings = GlobalDataHelper.Load<AppConfig>(); |
if you prefer async:
1 | var settings = await GlobalDataHelper.LoadAsync<AppConfig>(); |
CryptographyHelper
some method for generate hash and encrypt files
Generate Hash
MD5
1 | string text = CryptographyHelper.GenerateMD5("mahdi"); |
VerifyMD5
1 | bool verify = CryptographyHelper.VerifyMD5("F9C24B8F961D48841A9838CCA5274D8D", "mahdi"); |
SHA256
1 | string text = CryptographyHelper.GenerateSHA256("mahdi"); |
SHA256 for File
1 | string text = CryptographyHelper.GenerateSHA256FromFile(@"D:\test.zip"); |
VerifySHA256
1 | bool verify = CryptographyHelper.VerifySHA256("2e0af263c88c69ecc23a51a76b7a2442ed6a9dff080f275a27ec486d1a0e0148", "mahdi"); |
Encrypt & Decrypt Strings
AES
1 | string enc = CryptographyHelper.EncryptStringAES("Mahdi", "password"); |
Base64
1 | string enc = CryptographyHelper.EncryptStringBase64("Mahdi"); |
RSA
first Create your public and private key
1 | var keys = CryptographyHelper.GenerateRSAKey(); |
Then encrypt and decrypt strings
1 | string enc = CryptographyHelper.EncryptStringRSA("Mahdi", keys.PublicKey); |
Encrypt & Decrypt Files
AES
1 | CryptographyHelper.EncryptFileAESAsync(sourcefile, destination, "password"); |
RSA
first Create your public and private key
1 | var keys = CryptographyHelper.GenerateRSAKey(); |
Then encrypt and decrypt files
1 | CryptographyHelper.EncryptFileRSAAsync(inputFilePath, outputFilePath, keys.PublicKey); |
Read and Write RSA Key
We have methods for reading and writing public and private keys from/to files
1 | // export public key to file |
INIHelper
InIHelper class is for working with ini file in simple way.
Add Value
you can use default section and path
1 | InIHelper.AddValue("key1", "test1"); |
Read Value
1 | MessageBox.Info(InIHelper.ReadValue("key1")); |
Delete Key
you can delete a key or section
1 | InIHelper.DeleteKey("key4", "mySection"); |
Exist Key
you can check if a key exist or not
1 | MessageBox.Info(InIHelper.IsKeyExists("key4", "mySection")); |
RegistryHelper
RegistryHelper class is for working with windows Registry
Add Key
value can be everything (bool, int, string,…), default RegistryKey is CurrentUser for other type you need Admin Access
1 | RegistryHelper.AddOrUpdateKey("myKey", "myFolder", value); |
Get Key
you can cast value to any type you want
1 | RegistryHelper.GetValue<bool>("myKey", "myFolder"); |
Delete Key
default value for IsDeleteSubKey is False
1 | bool result = RegistryHelper.DeleteKey("myKey", "myFolder"); |
Delete SubKey Tree
1 | bool result = RegistryHelper.DeleteSubKeyTree("subkey", "myFolder"); |
UpdateHelper
you can use UpdateHelper for checking application updates from github release page
Methods
Method | Description |
---|---|
CheckUpdateAsync | Use the GitHab Release Page to check if the update is available |
CheckUpdate | Use the GitHab Release Page to check if the update is available |
first you must create a new release tag in github repository:
tag version must be in this format : 1.0.0.0
now we can check for update with github username and github repository
1 | var ver = await UpdateHelper.CheckUpdateAsync("ghost1372", "HandyControls"); |
ApplicationHelper
IsSingleInstance
you can use IsSingleInstance for Run only one Instance of the program at a time.
This method returns a true or false value If the value is true, it means that a Instance of the program is running and automatically closes the program and Brings Window To Front
Example:
1 | if(!ApplicationHelper.IsSingleInstance()) |
If for any reason the method does not work properly, specify the name of the program manually
1 | ApplicationHelper.IsSingleInstance("myApplication"); |
IsAdministrator
Check if Running Application runs with admin access or not
1 | if(ApplicationHelper.IsAdministrator()) |
BringWindowToFront
You can use this method to find and focus MainWindow that is running
1 | ApplicationHelper.BringWindowToFront(); |
StartProfileOptimization
Faster application execution at startup by caching
1 | ApplicationHelper.StartProfileOptimization(); |
StartProfileOptimization is not Available in .Net 4.0
IsConnectedToInternet
Check if the internet is connected or not
1 | bool IsConnected = ApplicationHelper.IsConnectedToInternet(); |
GetAbsoluteUri
Get Absolute Uri path
1 | var uri = ApplicationHelper.GetAbsoluteUri("myApp", @"Resource/Theme.xaml"); |
RegisterContextMenuToDirectory
Register Context Menu in Windows Directory
1 | string command = $"\"{Process.GetCurrentProcess().MainModule.FileName}\" \"%1\""; |
for UnRegister Context Menu from Windows Directory
1 | bool result = ApplicationHelper.UnRegisterContextMenuFromDirectory("MyApp"); |
RegisterContextMenuToFile
Register Context Menu in Windows File
1 | string command = $"\"{Process.GetCurrentProcess().MainModule.FileName}\" \"%1\""; |
for UnRegister Context Menu from Windows File
1 | ApplicationHelper.UnRegisterContextMenuFromFile("MyApp"); |
RegisterContextMenuToBackground
Register Context Menu in Windows Background
1 | string command = $"\"{Process.GetCurrentProcess().MainModule.FileName}\" \"%1\""; |
for UnRegister Context Menu from Windows File
1 | ApplicationHelper.UnRegisterContextMenuFromBackground("MyApp"); |
RegisterContextMenuToDrive
Register Context Menu in Windows Background
1 | string command = $"\"{Process.GetCurrentProcess().MainModule.FileName}\" \"%1\""; |
for UnRegister Context Menu from Windows File
1 | ApplicationHelper.UnRegisterContextMenuFromDrive("MyApp"); |
All of the above methods have an additional method, That contain Cascade
Use these methods when you want to create a Cascade Context Menu.
Registring and Unregsitring
Registering and UnRegistering ContextMenu To Drive and Cascade
Mode requires Administrator Access.
Restart
Restart Application
1 | ApplicationHelper.Restart(); |
GetExecutablePathNative
this is a P/Invoke Method to get current application executable path, this method work fine in .net framework and .net core in portable and publish mode release.
1 | var path = ApplicationHelper.GetExecutablePathNative(); |
GetExecutablePath
this is a Assembly based Method to get current application executable path, this method work fine in .net framework and .net core in portable and publish mode release. for .net 5.0, GetExecutablePathNative
method is used in this method.
1 | var path = ApplicationHelper.GetExecutablePath(); |
SendMessageToAnotherProcess and ListenToReceiveMessageFromAnotherProcess
if you want to send message between different applications you can use this native method
1 | ApplicationHelper.SendMessageToAnotherProcess("MainWindowTitle", "Message"); |
now You must register a listener to receive the message
1 | private void MainWindow_Loaded(object sender, RoutedEventArgs e) |
ColorHelper
GetColorFromBrush
Get Color from LinearGradientBrush, SolidColorBrush and Brush
1 | var color = ApplicationHelper.GetColorFromBrush(brush); |
GetColorFromString
Creates a Color from a XAML color string.
1 | var color = GetColorFromString("#3260a8"); |
GetHexFromColor
Get Hex Code from Color
1 | var color = GetHexFromColor(color); |
GetHexFromBrush
Get Hex Code from Brush
1 | var color = GetHexFromBrush(brush); |
OSVersionHelper
get os version
1 | OSVersionHelper.GetOSVersion(); |
Methods
Methods |
---|
IsWindowsNT |
IsWindows7 |
IsWindows7_OrGreater |
IsWindows8 |
IsWindows8_OrGreater |
IsWindows81 |
IsWindows81_OrGreater |
IsWindows10 |
IsWindows10_OrGreater |
IsWindows10_1507 |
IsWindows10_1507_OrGreater |
IsWindows10_1511 |
IsWindows10_1511_OrGreater |
IsWindows10_1607 |
IsWindows10_1607_OrGreater |
IsWindows10_1703 |
IsWindows10_1703_OrGreater |
IsWindows10_1709 |
IsWindows10_1709_OrGreater |
IsWindows10_1803 |
IsWindows10_1803_OrGreater |
IsWindows10_1809 |
IsWindows10_1809_OrGreater |
IsWindows10_1903 |
IsWindows10_1903_OrGreater |
IsWindows10_1909 |
IsWindows10_1909_OrGreater |
IsWindows10_2004 |
IsWindows10_2004_OrGreater |
IsWindows10_2009 |
IsWindows10_2009_OrGreater |
GenericCompare
Check if there is an specific item in the collection or not
1 | var model = new Model |
DispatcherHelper
Run codes in Main Thread
1 | DispatcherHelper.RunOnMainThread(() => |
ConvertHelper
BytesToBitmapImage
convert byte[]
to BitmapImage
1 | var bitmap = BytesToBitmapImage(bytes); |
BitmapImageToBytes
convert BitmapImage
to byte[]
1 | var bytes = BitmapImageToBytes(bitmap); |
BitmapImageToBase64
convert BitmapImage
to Base64
1 | var base64 = BitmapImageToBase64(bitmap); |
Base64ToBitmapImage
convert Base64
to BitmapImage
1 | var bitmap = Base64ToBitmapImage(base64); |
ToFileSize
Translate numeric file size in bytes to a human-readable shorter string format.
1 | string fileSize = ToFileSize(564654644); |
XmlHelper
Serialize and Deserialize properties of a class to xml file.
1 | Person person = new Person() { Name = "Mahdi", Age = 27 }; |
WindowHelper
DetermineIfInLightThemeMode
you can determine if windows theme is light or Dark
1 | bool isLight = WindowHelper.DetermineIfInLightThemeMode(); |
MicaHelper
ApplyMicaEffect
You can enable mica material for Windows
You do not need to use this helper to activate mica on hc:Window, hc:GlowWindow and BlurWindow, Mica is available on these windows by default through ApplyBackdropMaterial
Property.
Use this helper for System.Windows.Window
or any other type
Mica only works on Windows 11 and above
1.First we need to Create a WindowChrome and then change the background to Transparent
1 | public MainWindow() |
- Now we can Activate mica
1 | MicaHelper.ApplyMicaEffect(this); |
or you can use another method with support dark mode
1 | MicaHelper.ApplyMicaEffect(handle, isDark); |
If your Windows uses a dark theme, you need to set isDark
to true
if you need to remove Mica you can use RemoveMicaEffect method
1 | MicaHelper.RemoveMicaEffect(); |
if you want to know that mica effect is applied you can use:
1 | bool isApplied = MicaHelper.IsMicaEffectApplied; |
TaskbarHelper
You can control your app Taskbar Progressbar with this helper class
for changing Progressbar state:
1 | TaskbarHelper.SetProgressState(handle, TaskbarStates.Error); |
for changing Progressbar value:
1 | TaskbarHelper.SetProgressValue(handle, 10, 100); |