ENGR/CS 101 CS Session Lecture 13 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2012 and open the Substitution Cipher Project Has everyone finished the program from last class so that it can encipher and decipher using the Vigenere cipher? Lecture 13 ENGR/CS 101 Computer Science Session 1 Outline Problem: Loading messages from and saving messages to files Lecture 13 File streams Open/Save Dialogs ENGR/CS 101 Computer Science Session 2 Problem: Loading & Saving Messages Typing in long messages is tedious and error prone. Also, message often come in textfiles. Keeping a record of the resulting messages would require writing down the messages on a piece of paper, also tedious and error prone. We would like to enhance our project to be able to read messages from a file and write the results to a textfile. Lecture 13 ENGR/CS 101 Computer Science Session 3 GUI Interface Add four buttons to the GUI to be clicked to do the following actions. Set their Name properties as indicated Load Plaintext - loadPlainBtn Save Plaintext - savePlainBtn Load Ciphertext - loadCipherBtn Save Ciphertext - saveCipherBtn Style them however you like. Lecture 13 ENGR/CS 101 Computer Science Session 4 Reading from a File Double-click on the Load Plaintext button. The handler for this button must read a plaintext message from a file and load it into the plaintext box. A file stream is a program object that is opened (i.e., attached to a physical file) when it is created. StreamReader objects are used to read data from a file. Lecture 13 ENGR/CS 101 Computer Science Session 5 OpenFileDialog An OpenFileDialog is the window that pops up when a user wants to open a file. It is used to connect a file stream to a physical file. Lecture 13 ENGR/CS 101 Computer Science Session 6 OpenFileDialog An OpenFileDialog is declared and initialized as follows: OpenFileDialog fileChooser = new OpenFileDialog(); To make the dialog pop up and get the file name from the user, call the ShowDialog method and save the result in a DialogResult variable DialogResult result = fileChooser.ShowDialog(); Lecture 13 ENGR/CS 101 Computer Science Session 7 OpenFileDialog If a user clicks on the Cancel button, our application must go back to waiting, so we test the DialogResult. // Check if user clicked Cancel if (result == DialogResult.Cancel) { return; // Go back to waiting } Lecture 13 ENGR/CS 101 Computer Science Session 8 StreamReader Now we create a StreamReader object using the chosen file name, then read the entire file into the plaintext box. // Open (attach) the file chosen in the dialog StreamReader fileInput = new StreamReader (fileChooser.FileName); // Read entire file contents into plaintext box plaintextBox.Text = fileInput.ReadToEnd(); Lecture 13 ENGR/CS 101 Computer Science Session 9 StreamReader The squiggly red lines under "StreamReader", means that the program needs to import the library that defines StreamReader. Do this by right-clicking on "StreamReader", selecting "Resolve", then "using System.IO". Lecture 13 ENGR/CS 101 Computer Science Session 10 StreamReader All file streams should be closed (i.e. detached from the physical file). fileInput.Close(); // Detach the physical file Finally, the ciphertext box should be cleared, since whatever was in it is no longer related to what is now in the plaintext box ciphertextBox.Text = ""; // Clear ciphertext box Lecture 13 ENGR/CS 101 Computer Science Session 11 In-class Exercise, Part 1 Test the program. Create a message file by using FILE->New, select Text File. Then use FILE->Save As to give it a name like "message.txt" and put it in the project folder. Click on the Load Plaintext button. An OpenFileDialog should pop up. Browse to your message file and click Open. The text in the file should appear in the plaintext box. Lecture 13 ENGR/CS 101 Computer Science Session 12 In-class Exercise, Part 1 Double-click on the Load Ciphertext button Write the click handler code for this button. It is almost exactly the same as the code for the Load Plaintext button. The only difference is that the file contents are put into the ciphertext box (instead of the plaintext box) and the plaintext box is cleared. Test you program again. Lecture 13 ENGR/CS 101 Computer Science Session 13 Writing to a File Double-click on the Save Ciphertext button This handler must write a ciphertext message to a file. StreamWriter objects are used to write data to a file. Lecture 13 ENGR/CS 101 Computer Science Session 14 SaveFileDialog Getting a file name for saving a file is done with a SaveFileDialog. It works exactly the same way as the OpenFileDialog for getting a file name from the user. (I.e., it is the same code with SaveFileDialog instead of OpenFileDialog.) Lecture 13 ENGR/CS 101 Computer Science Session 15 StreamWriter Now we create a StreamWriter object using the chosen file name, then write the Text of the ciphertext box to the file: // Open (attach) the file chosen in the dialog StreamWriter fileOutput = new StreamWriter (fileChooser.FileName); // Write ciphertext to file fileOutput.WriteLine (ciphertextBox.Text); fileOutput.Close(); // Detach the physical file Lecture 13 ENGR/CS 101 Computer Science Session 16 In-class Exercise, Part 2 Test the program. After loading a plaintext message and enciphering it, click on the Save Ciphertext button. A SaveFileDialog should pop up. Type in a file name like "secret.txt", make sure it will be saved into the project folder, and click Save. You can look at the saved file by using FILE >Open->File. Lecture 13 ENGR/CS 101 Computer Science Session 17 In-class Exercise, Part 2 Double-click on the Save Plaintext button and write the click handler for it. It is almost exactly the same as the code for the Save Ciphertext button. The only difference is that plaintextBox.Text is saved to the file instead. Lecture 13 ENGR/CS 101 Computer Science Session 18