- Open Visual Studio 2017.
- Now open the solution we built from the previous recipe.
- The Solution Explorer should look like this:
- Now, let's select the solution name and right-click.
- From the menu, select Add | New Project.
- In the New Project dialog box, expand the Visual C# node and select Windows Classic Desktop in the left-hand pane.
- In the right-hand pane, select Windows Forms App (.NET Framework):
- Now, in the Name: textbox, type Chapter3.SecureFile.SecureWinApp, leave the Location: textbox as it is, and click OK.
- Now, the Solution Explorer should look like this:
- Let's rename Form1.cs as MainForm.cs, by selecting it and pressing F2.
- Answer Yes in the confirmation box to confirm the changing of the main class name as well.
- Now select the Windows form from the designer window.
- Drag and drop two Buttons in the form.
- Also drag and drop an OpenFileDialogBox control in the Windows form.
- Change the properties of the previous controls as per this table:
Control |
Property |
Value |
Button |
Name |
EncryptButton |
Button |
Text |
Encrypt |
Button |
Name |
DecryptButton |
Button |
Text |
Decrypt |
OpenFileDialogBox |
Name |
OpenDialog |
OpenFileDialogBox |
Filter |
Text Files|*.txt |
- Now, your designer area should look like this:
- Now select the Chapter3.SecureFile.SecureWinApp label in the Solution Explorer and expand it.
- Right-click on the References label and select Add Reference.
- Click on the Projects node in the Reference Manager dialog box.
- Check the check box in front of the Chapter3.SecureFile.CryptLib label from the project list in the right-hand pane.
- Click OK.
- Now switch back to the design window and double-click on the Encrypt button to open the code window.
- In the code window, scroll up to the top and add the following using directive as the last line of the using block:
using Chapter3.SecureFile.CryptLib;
- Now scroll down to the Encrypt button, click, and add the following code in between the curly brackets:
if (OpenDialog.ShowDialog() == DialogResult.OK)
{
var textFileName = OpenDialog.FileName;
var secureFile = new CryptFile(textFileName);
secureFile.EncryptFile();
MessageBox.Show("File encrypted", "Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
- Now switch back to the designer window by clicking on the MainForm.cs [designer] tab.
- Double-click on the Decrypt button to reach the code for that button click.
- Add the following code in between the curly brackets of the button click code:
if (OpenDialog.ShowDialog() == DialogResult.OK)
{
var textFileName = OpenDialog.FileName;
var secureFile = new CryptFile(textFileName);
secureFile.DecryptFile();
MessageBox.Show("File decrypted", "Information",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
- Now press F5 to execute the code (make sure the classic Windows application project is the default project)
- Press the Encrypt button and select a file:
- Click Open and click OK in the information box.
- Now open Windows Explorer and navigate to the location of the file that you just encrypted.
- You will notice a lock on the file.
- Now switch back to your app, click on the Decrypt button, and follow the same steps as before.
- Now the lock is removed from the file.
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.