【发布时间】:2013-11-30 10:51:44
【问题描述】:
以下两个功能有问题。两者都有一个相同的基本方案,但第一个工作,第二个导致标记行异常(“文件被另一个进程使用”)。
// 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);
}
}
我在读写后尝试了一个额外的 Close() ,但这也不起作用。 希望有人能帮忙。
谢谢
托尔斯滕
【问题讨论】: