title: c# File.Delete - File being used by another process
body: <p>I have a small email-program which does encryption. Below is just a summary of the program:</p>
  
  <pre><code>private void sendEmailButton_Click(object sender, EventArgs e)
  {
      else
      {    
          //////////////////////////////////////////////////////////////////////////
          if (encryptEverythingCheckBox.Checked)
          {
              encryptAll();
          }
          //////////////////////////////////////////////////////////////////////////
  
          // Email credentials network codes blahblah
          // Assign the sender's email address to MailAddress function
          MailAddress mailAddress = new MailAddress(username);
          // Tells the recipent the sender's email
          mailMessage.From = mailAddress;
          // Username &amp; Password of your email address
          System.Net.NetworkCredential networkCredential;
          networkCredential = new System.Net.NetworkCredential(username, password);
          // Enable SSL to encypt the connection
          smtpClient.EnableSsl = true;
          // Disable the use of default credentials
          smtpClient.UseDefaultCredentials = false;
          // Specify your own credential
          smtpClient.Credentials = networkCredential;
          //port number and send email blahblahblah
          deleteEncryptedFile();
      }
  }
  </code></pre>
  
  <p>So the problem I'm having now is regarding the void method of deleteEncryptedFile() and encryptAll(). Below are the codes:</p>
  
  <pre><code>public void deleteEncryptedFile()
  {
      if (File.Exists(@"C:\EncryptedFile.pgp"))            
          File.Delete(@"C:\EncryptedFile.pgp");            
  }
  
  public void encryptAll()
  {
      OpenFileDialog openFileDialog1 = new OpenFileDialog();
      openFileDialog1.InitialDirectory = "c:\\";
      openFileDialog1.RestoreDirectory = true;
      openFileDialog1.Title = "CHOOSE RECIPENT'S PUBLIC KEY";
  
      if (openFileDialog1.ShowDialog() == DialogResult.OK)
      {
          invisibleTextBox.Text = openFileDialog1.FileName.ToString();
          string encryptedbodymessage = pgp.EncryptString(messageRichTextBox.Text, new FileInfo(@invisibleTextBox.Text));
          messageRichTextBox.Text = "";
          messageRichTextBox.Text = encryptedbodymessage;
  
          if (attachmentTextBox.Text != "")
          {
              bool asciiArmor = false;
              bool withIntegrityCheck = false;
              pgp.EncryptFile(@attachmentTextBox.Text, @invisibleTextBox.Text, @"C:\EncryptedFile.pgp", asciiArmor, withIntegrityCheck);
              invisibleTextBox.Text = "";
              mailAttachment = new Attachment(@"C:\EncryptedFile.pgp");
          }
      }
  }
  </code></pre>
  
  <p>So when the send button is clicked and files are encrypted and sent, I want to remove it from my computer. So I run the method <code>deleteEncryptedFile</code> to remove the EncryptedFile.pgp from my computer. But i kept getting this message that is saying:</p>
  
  <blockquote>
    <p>"The process cannot access the file 'C:\EncryptedFile.pgp' because it is being used by another process."</p>
  </blockquote>
  
  <p>But the only "other process" that I can think of is the encryption method (encryptAll()). But shouldn't that had been done? Please advice how I can solve this problem?</p>