简体   繁体   中英

c# exception file is being used by another process

i have trouble with the following two functions. Both have a indentical basic scheme but first one work, second one causes an exception at marked line("File is used by another process").

    // this works
    public static void EncryptFile(string FileName)
    {
        string ToEncrypt = null;

        using(StreamReader sr = new StreamReader(FileName))
        {
            ToEncrypt = sr.ReadToEnd();
        }
        using(StreamWriter sw = new StreamWriter(FileName, false))
        {
            string Encrypted = Encrypt(ToEncrypt, true);
            sw.Write(Encrypted);
        }
    }
    // this works not - see commented lin
    public static void DecryptFile(string FileName)
    {
        string ToDecrypt = null;

        using (StreamReader sr = new StreamReader(FileName))
        {
            ToDecrypt = sr.ReadToEnd();
        }
        // here comes the exception
        using (StreamWriter sw = new StreamWriter(FileName, false))
        {
            string Decrypted = Decrypt(ToDecrypt, true);
            sw.Write(Decrypted);
        }
    }

I have tried with an additional Close() after read and write, but this works not too. I hope, somebody can help.

Thanks

Torsten

Is the function called from multiple threads? If yes you may want to declare a static object on class level and place a lock statement around the entire body of that method. Like this:

private static Object syncObject = new Object()
// this works not - see commented lin
public static void DecryptFile(string FileName)
{
    lock(syncObject)
    {
        string ToDecrypt = null;

        using (StreamReader sr = new StreamReader(FileName))
        {
            ToDecrypt = sr.ReadToEnd();
        }
        // here comes the exception
        using (StreamWriter sw = new StreamWriter(FileName, false))
        {
            string Decrypted = Decrypt(ToDecrypt, true);
            sw.Write(Decrypted);
        }
    }
}

Also could you, just for fun, comment the StreamReader statement and try to run the method again? If it still doesn't work, check if you've that file open in a texteditor or something alike by using ProcessExplorer or something similiar.

edit could you comment the StreamReader part? So that it looks like this:

 public static void DecryptFile(string FileName)
 {

    //string ToDecrypt = null;

    //using (StreamReader sr = new StreamReader(FileName))
    //{
     //   ToDecrypt = sr.ReadToEnd();
    //}
    // here comes the exception
    using (StreamWriter sw = new StreamWriter(FileName, false))
    {
        string Decrypted = Decrypt(ToDecrypt, true);
        sw.Write(Decrypted);
    }

}

also could you try to open an exclusive FileStream on that file before the StreamReader and once after the StreamReader but before the StreamWriter? http://msdn.microsoft.com/de-de/library/tyhc0kft%28v=vs.110%29.aspx

Also could you try and use another file for that method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM