File.Delete无法删除文件,因为它正在被另一个进程使用。不确定如何解决此问题 [英] File.Delete cannot delete a file because it is being used by another process. Not sure how to fix this

查看:852
本文介绍了File.Delete无法删除文件,因为它正在被另一个进程使用。不确定如何解决此问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,其中有一个网络表单,该表单可以动态生成.csv文件,然后使用GnuPG进行加密。加密过程起作用,并且加密文件在同一目录中生成。接下来,在文件加密之后,我需要删除常规的.csv文件。



我已经使用file.delete来做到这一点,但我遇到了错误该进程无法访问文件'FILEPATH / FILENAME.EXT',因为它正在被另一个进程使用。我不确定是否将代码放在错误的区域。



  public void cryptoPGP(string fileName)$ b >>有人可以建议我该怎么做吗? $ b {
try
{
string sCommandLine = String.Format(@-r CERT NAME -e {0},fileName);
//lblError.Text =< pre> + sCommandLine +< / pre>;

字符串userPwd = COOLPWD;
System.Security.SecureString pwd = new System.Security.SecureString();
foreach(userPwd.ToCharArray()中的char c)
{
pwd.AppendChar(c);
}

System.Diagnostics.ProcessStartInfo info =新系统.Diagnostics.ProcessStartInfo();
info.Arguments = sCommandLine;
info.Domain = DOMAINNAME;
info.FileName = C:\\实用工具\\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 =新的System.Diagnostics.Process();
proc.StartInfo = info;
proc.Start();
lblError.Text = proc.StandardOutput.ReadToEnd();

System.IO.File.Delete(fileName);

}
catch(异常例外)
{
lblError.Text = ex.Message;
//writeToLog(ex.Message,);
}
}

//编写错误日志的方法
private void writeToLog(string strMessage,string strMethod)
{
使用( System.IO.StreamWriter文件=新System.IO.StreamWriter(@ C:\\Log.txt,true))
{
file.WriteLine(string.Format( {0 }-{1}-{2},DateTime.Now,strMethod,strMessage));
}
}
//写入错误日志的结束方法

此外,这是创建文件的过程:

 字符串fileName = @ c:\DIR\DIR\ \ + pt.SelectedItem.Value + pcf +- + usname +- + sname.Text +- + cdt + .csv; 
字符串行= 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。文本+, + +, + 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);

cryptoPGP(fileName);


解决方案

我会说GnuPG可能仍在使用



您需要等待进程结束才能删除文件,即添加:

  proc.WaitForExit(); 

直接在

 <之前code> System.IO.File.Delete(fileName); 


I have a project where I have a web form that generates a .csv file on the fly and then is encrypted using GnuPG. The encryption process works and the encrypted file is generated in the same directory. Next, after the file is encrypted, I need for the regular .csv file to be deleted.

I have used file.delete to do this but I am getting the error "The process cannot access the file 'FILEPATH/FILENAME.EXT' because it is being used by another process. I am unsure if I placed the code in the wrong area.

Can anyone suggest to me what I should do? Here is the relevant code.

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

Also, here is the process to create the file:

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

解决方案

I would say it is likely that GnuPG is still using the file when you try to delete it.

You need to wait for the process to end before doing the deletion, i.e. add:

proc.WaitForExit(); 

directly before

System.IO.File.Delete(fileName); 

这篇关于File.Delete无法删除文件,因为它正在被另一个进程使用。不确定如何解决此问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆