Open IP Office Backup files – Part 3

In my last post I promised to show you how to use the registry to store settings and to read them. Only with that topic it would be possible to create multiple posts. I will only show you the most important options in short.

Furthermore I’ve read about using classes and created some structure in the code.

Classes

A class can contain methods and variables. Methods are parts of the program, which can be called either with or without additional parameters. Methods are sometimes also called functions and can be compared with the functions within a Powershell script.

Two methods I created are for the menus. After showing the options and the interpretation of the user input, I call another method, i.e. the submenu for the settings.

If a class is declared as “public”, it is possible to call the methods and variables of that class even from other classes. You can say, that those variables and methods are then called using their full path. So you can call for example the main menu with Menu.MainMenu() and a variable with GlobalVariables.programFilesX86.

Classes in my program

Until now i created the following classes in my program:

  • Program
    • The default class, that also contains the main function Main()
  • GlobalVariables
    • Contains all the variables, that can be used in the whole program
  • Utilities
    • Contains several Methods, that are used as helpful functions at other positions. With that methods it is possible to compress the code a little bit.
  • Menu
    • Contains the menus
  • Settings
    • Contains several methods to set and read settings
  • Exec
    • Will contain the methods that give the program it’s value. For example to get the available backups. I didn’t find a better name than Exec. Ideas?

Registry

Last time I promised to give you some insights about my excursion into the registry. It would be possible to write quite a lot about that. I will keep it short.

Create a key

During the first start of the program, there are usually no corresponding entries in the registry available. Those have to be created first.

RegistryKey fwregkey = Registry.CurrentUser.CreateSubKey(@"Software\Florian Wilke");

In this example I create a variable “fwregkey” and create a subkey “Florian Wilke” under “HKEY_CURRENT_USER\Software\”. Here can the settings of further programs be stored.

The variable “fwregkey” can later for example be used, to create new parameters with it’s values or to read existing values.

Create parameter/value pairs

Under the just created key i will now create two parameters with their values.

// Set two new values under the newly created subkey
fwregkey.SetValue("Website", "https://www.fwilke.com");
fwregkey.SetValue("Author", "Florian Wilke");

SetValue as a functions to the variable does this for me. As arguments here are i.e. the parameter (Website) and the value (https://www.fwilke.com) used.

Open a key

If the key already exists in registry and you want to do further actions (write valies, read values, …), you can open the key the same way as I created it.

// Open Registry Subkey
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software");

In the first line of code I open the key HKEY_CURRENT_USER\Software\ and keep it in the variable “key” for further usage.

// Open the named subkey
RegistryKey readkey = key.OpenSubKey("Florian Wilke");

With the second line of code I open the subkey of key and keep it in the variable “readkey” for further usage.

Read values

// Get the value of "Website" and transform the result to.string
string fwwebsite = readkey.GetValue("Website").ToString();
Console.WriteLine(fwwebsite);

After I opened the key “HKEY_CURRENT_USER\Software\Florian Wilke\” in “readkey”, I am able to read the value of “Website”. To output this value, I convert it to a string and store it within the variable “fwwebsite”.

There is another possibility to read a value from registry.

public class GlobalVariables
{
    // Define registry path to search for and to store setting
    public static string progregkeypath = @"HKEY_CURRENT_USER\Software\Florian Wilke\IPOBackupAccess\";
}
public class Testclass
{
    string test = Registry.GetValue(GlobalVariables.progregkeypath, "ManagerProgramPath", null).ToString();
    Console.WriteLine("The value of test is: " + test);
}

Here I read the value with “Registry.GetValue” without the need to create an additional variable to open the key. The value is then stored in a variable and can be used further.

Close a key again

After opening the key through a variable or creating it, you can also close it.

readkey.Close();
key.Close();

How will it go on?

In the future I will take care of the settings. While I already checked and created settings automatically when needed, in the next step I will either show you the settings and give you the opportunity to let you adjust the settings. Just showing the settings will not be the problem. The challenge will be to also let you chose specific settings to adjust like in a menu.

Stay curious, I am curious, too.

To stay in touch if I publish any update, just subscribe to my newsletter.

If you need further help with IP Office you can contact me through my main website: https://www.fwilke.com/home

Leave a Reply

Your email address will not be published.