2

その場で.csvファイルを生成し、GnuPGを使用して暗号化するWebフォームがあるプロジェクトがあります。暗号化プロセスが機能し、暗号化されたファイルが同じディレクトリに生成されます。次に、ファイルを暗号化した後、通常の.csvファイルを削除する必要があります。

file.deleteを使用してこれを実行しましたが、「別のプロセスで使用されているため、プロセスはファイル'FILEPATH/FILENAME.EXT'にアクセスできません。コードを間違った領域に配置したかどうかわかりません。 。

誰かが私に何をすべきかを提案できますか?関連するコードは次のとおりです。

public void encryptPGP(string fileName)
{
    try
    {
        string sCommandLine = String.Format(@"-r ""CERT NAME"" -e ""{0}""", fileName);
       //lblError.Text = "<pre>" + sCommandLine + "</pre>";

        string userPwd = "COOLPWD";
        System.Security.SecureString pwd = new System.Security.SecureString();
        foreach (char c in userPwd.ToCharArray())
        {
            pwd.AppendChar(c);
        }

        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
        info.Arguments = sCommandLine;
        info.Domain = "DOMAINNAME";
        info.FileName = "C:\\Utilities\\GnuPG\\App\\gpg.exe";
        info.Password = pwd;
        info.UserName = "USERNAME";
        info.UseShellExecute = false;
       info.RedirectStandardOutput = true;
       info.CreateNoWindow = true;
        info.WorkingDirectory = "C:\\Utilities\\GnuPG\\App\\";

        //writeToLog(info.FileName, "App");
        //writeToLog(sCommandLine, "Args");

       System.Diagnostics.Process proc = new System.Diagnostics.Process();
       proc.StartInfo = info;
        proc.Start();
       lblError.Text = proc.StandardOutput.ReadToEnd();

    System.IO.File.Delete(fileName);

    }
    catch (Exception ex)
    {
       lblError.Text = ex.Message;
        //writeToLog(ex.Message, "");
    }
}

// method for writing error log
private void writeToLog(string strMessage, string strMethod)
{
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\Log.txt", true))
    {
        file.WriteLine(string.Format("{0} - {1} - {2}", DateTime.Now, strMethod, strMessage));
    }
}
// end method for writing error log

また、ファイルを作成するプロセスは次のとおりです。

        string fileName = @"c:\DIR\DIR\" + pt.SelectedItem.Value + pcf + "--" + usname + "--" + sname.Text + "--" + cdt + ".csv";
    string lines = DropDownList3.SelectedItem.Value + "," + DropDownList8.SelectedItem.Value + "," + DropDownList1.SelectedItem.Value + "," + TextBox25.Text + "," + ssn.Text + "," + TextBox13.Text + "," + Lastname.Text + "," + firstname.Text + "," + " " + "," + TextBox1.Text + "," + TextBox3.Text + "," + TextBox4.Text + "," + TextBox5.Text + "," + TextBox6.Text + "," + TextBox9.Text + "," + TextBox10.Text + "," + TextBox11.Text + "," + TextBox2.Text + "," + " " + "," + TextBox22.Text + "," + TextBox26.Text + "," + TextBox29.Text + "," + TextBox19.Text + "," + TextBox27.Text + "," + TextBox30.Text + "," + TextBox24.Text + "," + TextBox28.Text + "," + TextBox8.Text + "," + DropDownList7.SelectedItem.Value + "," + TextBox38.Text + " " + TextBox34.Text + "," + TextBox33.Text + "," + TextBox41.Text + "," + TextBox35.Text + "," + TextBox36.Text + "," + TextBox37.Text + "," + TextBox54.Text + "," +" "+"," + TextBox12.Text;
    System.IO.File.WriteAllText(fileName, lines);

    encryptPGP(fileName);
4

2 に答える 2

3

ファイルを削除しようとすると、GnuPGがまだファイルを使用している可能性があります。

削除を行う前に、プロセスが終了するのを待つ必要があります。つまり、次を追加します。

proc.WaitForExit(); 

直前

System.IO.File.Delete(fileName); 
于 2012-10-02T13:54:45.003 に答える
2

ファイルを作成するときに書き込みストリームを閉じましたか?それだけです。ストリームを閉じてから削除する必要があります。

このファイルを作成するコードには、説明のストリームが必要です。Closeメソッドがあるかどうかを確認します。そもそもファイルを作成するコードを投稿できますか?

于 2012-10-02T13:51:21.993 に答える