簡體   English   中英

File.Delete無法刪除文件,因為它正在被另一個進程使用。 不確定如何解決此問題

[英]File.Delete cannot delete a file because it is being used by another process. Not sure how to fix this

我有一個項目,其中有一個網絡表單,該表單可以動態生成.csv文件,然后使用GnuPG進行加密。 加密過程起作用,並且加密文件在同一目錄中生成。 接下來,在文件加密之后,我需要刪除常規的.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);

我想說的是,當您嘗試刪除文件時,GnuPG可能仍在使用該文件。

您需要等待該過程結束才能進行刪除,即添加:

proc.WaitForExit(); 

之前

System.IO.File.Delete(fileName); 

創建文件時是否關閉了寫流。 就是這樣。 您需要關閉流然后刪除。

無論您使用什么代碼創建該文件,都必須具有一些描述流。 檢查是否有關閉方法。 您可以首先發布創建文件的代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM