1

我有一个加密的小型电子邮件程序。以下只是该程序的摘要:

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 & 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();
    }
}

所以我现在遇到的问题是关于 deleteEncryptedFile() 和 encryptAll() 的 void 方法。以下是代码:

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");
        }
    }
}

因此,当单击发送按钮并加密并发送文件时,我想将其从计算机中删除。所以我运行了deleteEncryptedFile从我的计算机中删除 EncryptedFile.pgp 的方法。但我不断收到这条消息:

“该进程无法访问文件 'C:\EncryptedFile.pgp',因为它正被另一个进程使用。”

但我能想到的唯一“其他过程”是加密方法(encryptAll())。但不应该这样做吗?请建议我如何解决这个问题?

4

2 回答 2

8

尝试在删除过程之前处理邮件附件。

mailAttachment.Dispose();
于 2013-11-08T09:52:59.507 回答
-3

在删除您的加密文件之前,您可以验证然后删除。以下功能可以帮助您查找文件是否正在被另一个进程使用。

public static bool IsFileLocked(string fileName)
        {
            FileStream stream = null;
            try
            {
                stream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException)
            {
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
            return false;
        }
于 2013-11-08T09:22:51.070 回答