Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void showOutput()
         {
             if (!(string.IsNullOrWhiteSpace(InputTxt.Text))) // Make sure the textbox isnt empty
             {
                 SoundPlayer Send = new SoundPlayer("SOUND1.wav"); // Send Sound Effect
                 SoundPlayer Rcv = new SoundPlayer("SOUND2.wav"); // Recieve Sound Effect

                 // Show the user message and play the sound
                 addInMessage(InputTxt.Text);
                 Send.Play();

                 // Store the Bot's Output by giving it our input.
                 string outtt = bot.getOutput(InputTxt.Text);

                 if (outtt.Length == 0)
                 {
                     outtt = "I don't understand.";
                 }

                 //=========== Creates backup of chat from user and bot to the given location ============
                 FileStream fs = new FileStream(@"chat.log", FileMode.Append, FileAccess.Write);
                 if (fs.CanWrite)
                 {
                     byte[] write = System.Text.Encoding.ASCII.GetBytes(InputTxt.Text + Environment.NewLine + outtt + Environment.NewLine);
                     fs.Write(write, 0, write.Length);
                 }
                 fs.Flush();
                 fs.Close();
                 //=======================================================================================

                 // Make a Dynamic Timer to delay the bot's response to make it feel humanlike.
                 var t = new Timer();

                 // Time in milseconds - minimum delay of 1s plus 0.1s per character.
                 t.Interval = 1000 + (outtt.Length * 100);

                 // Show the "Bot is typing.." text
                 txtTyping.Show();

                 // disable the chat box white the bot is typing to prevent user spam.
                 InputTxt.Enabled = false;

                 t.Tick += (s, d) =>
                 {
                     // Once the timer ends

                     InputTxt.Enabled = true; // Enable Chat box

                     // Hide the "Bot is typing.." text
                     txtTyping.Hide();

                     // Show the bot message and play the sound
                     addOutMessage(outtt);
                     Rcv.Play();

                     // Text to Speech if enabled
                     if (textToSpeech)
                     {
                         reader.SpeakAsync(outtt);
                     }

                     InputTxt.Focus(); // Put the cursor back on the textbox
                     t.Stop();
                 };
                 t.Start(); // Start Timer

                 InputTxt.Text = ""; // Reset textbox
             }
         }