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 に答える