简体   繁体   English

File.Delete无法删除文件,因为它正在被另一个进程使用。 不确定如何解决此问题

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

I have a project where I have a web form that generates a .csv file on the fly and then is encrypted using GnuPG. 我有一个项目,其中有一个网络表单,该表单可以动态生成.csv文件,然后使用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. 接下来,在文件加密之后,我需要删除常规的.csv文件。

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. 我已经使用file.delete来做到这一点,但是却收到错误消息“该进程无法访问文件'FILEPATH / FILENAME.EXT',因为它正在被另一个进程使用。我不确定是否将代码放在错误的区域中。

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. 我想说的是,当您尝试删除文件时,GnuPG可能仍在使用该文件。

You need to wait for the process to end before doing the deletion, ie add: 您需要等待该过程结束才能进行删除,即添加:

proc.WaitForExit(); 

directly before 之前

System.IO.File.Delete(fileName); 

Have you closed the write stream when you create the file. 创建文件时是否关闭了写流。 That will be it. 就是这样。 You need to close the stream then delete. 您需要关闭流然后删除。

Whatever code you have that creates this file must have a stream of some description. 无论您使用什么代码创建该文件,都必须具有一些描述流。 Check if there is a Close method on it. 检查是否有关闭方法。 Can you post the code that creates the file in the first place? 您可以首先发布创建文件的代码吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 File.Delete无法删除文件,因为它正在被另一个进程使用 - File.Delete cannot delete a file because it is being used by another process File.Delete进程无法访问文件,因为它正在被另一个进程使用 - File.Delete the process cannot access the file because it is being used by another process File.Delete错误“该进程无法访问该文件,因为该文件正在被另一个进程使用” - File.Delete error “The process cannot access the file because it is being used by another process” File.Delete被另一个进程使用 - File.Delete being used by another process C# File.Delete,另一个进程正在使用的文件 - C# File.Delete, file being used by another process 位图上的File.Delete不起作用,因为该文件正在被另一个进程使用(我使用了bitmap.Dispose();) - File.Delete on bitmap doesn't work because the file is being used by another process (I used bitmap.Dispose();) 无法删除文件,因为它正被另一个进程使用 - cannot delete file because it's being used by another process 如何避免异常进程无法访问该文件,因为它正由另一个进程使用。 在.net - How to avoid exception The process cannot access the file because it is being used by another process. in .net 无法删除文件,因为它正由另一个进程使用 - Cannot delete file because it is being used by another process 错误:“该进程无法访问...,因为它正在被另一个进程使用。 删除图片中的“” - Error: “The process cannot access… because it is being used by another process. ” in Delete Image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM