【问题标题】:File.Delete cannot delete a file because it is being used by another process. Not sure how to fix thisFile.Delete 无法删除文件,因为它正被另一个进程使用。不知道如何解决这个问题
【发布时间】:2012-10-02 13:48:53
【问题描述】:

我有一个项目,其中有一个 Web 表单,它可以动态生成一个 .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);

【问题讨论】:

标签: c# file process delete-file


【解决方案1】:

我会说,当您尝试删除该文件时,GnuPG 可能仍在使用该文件。

您需要等待该过程结束后再进行删除,即添加:

proc.WaitForExit(); 

直接在之前

System.IO.File.Delete(fileName); 

【讨论】:

  • 这完全解决了我的问题。谢谢!
【解决方案2】:

您是否在创建文件时关闭了写入流。就是这样。您需要关闭流然后删除。

创建此文件的任何代码都必须有一些描述流。检查上面是否有 Close 方法。您可以发布首先创建文件的代码吗?

【讨论】:

  • 我对 c# 还很陌生,并且一直在学习。我该怎么做?我已经更新了上面的代码以显示文件的创建。谢谢!
猜你喜欢
  • 2014-05-26
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
相关资源
最近更新 更多