[英]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. 在读写之后,我尝试使用额外的Close(),但这不是很有效。 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? 您也可以只是出于娱乐目的而注释StreamReader语句,然后尝试再次运行该方法? 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.
如果仍然无法使用,请使用ProcessExplorer或类似工具检查您是否已在文本编辑器中打开该文件或类似文件。
edit could you comment the StreamReader part? 编辑您可以评论StreamReader部分吗? 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? 您还可以尝试在StreamReader之前,在StreamReader之后但在StreamWriter之前打开该文件的独占FileStream吗? http://msdn.microsoft.com/de-de/library/tyhc0kft%28v=vs.110%29.aspx
http://msdn.microsoft.com/de-de/library/tyhc0kft%28v=vs.110%29.aspx
Also could you try and use another file for that method? 还可以尝试为该方法使用另一个文件吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.