Datei.Löschen Sie können eine Datei nicht löschen, weil es verwendet wird von einem anderen Prozess. Nicht sicher, wie man dieses Problem beheben

Habe ich ein Projekt wo ich ein web-Formular generiert .csv-Datei on-the-fly und dann verschlüsselt mit GnuPG. Die Verschlüsselung funktioniert und die verschlüsselte Datei wird erzeugt in dem selben Verzeichnis. Als Nächstes, nachdem die Datei verschlüsselt ist, muss ich für die regelmäßige .csv-Datei gelöscht werden.

Habe ich verwendet-Datei.löschen, dies zu tun, aber ich bin immer der Fehler "Der Prozess kann nicht auf die Datei 'DATEIPFAD/DATEINAME.EXT', denn es wird von einem anderen Prozess verwendet. Ich bin nicht sicher, ob ich platziert den code in der falschen Gegend.

Kann jemand empfehlen mir, was ich tun sollte? Hier ist der relevante 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

Auch hier ist der Prozess um die Datei zu erstellen:

        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);
Sie können diese Lösung verwenden, stackoverflow.com/questions/13262548/...

InformationsquelleAutor user1714466 | 2012-10-02

Schreibe einen Kommentar