如何删除为正常 I/O 打开的文件?
我想解密文件。如果代码不正确,那么我需要删除输出文件。
我不能使用File.Delete()
,因为:
Windows NT 4.0 平台 注意:删除不会删除为正常 I/O 打开的文件或内存映射的文件。
try
{
FileStream fsIn = new FileStream(fileIn, FileMode.Open, FileAccess.Read, FileShare.Delete);
FileStream fsOut = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Delete);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(fsOut, alg.CreateDecryptor(), CryptoStreamMode.Write);
int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int bytesRead;
do
{
bytesRead = fsIn.Read(buffer, 0, bufferLen);
cs.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
cs.Close();
fsIn.Close();
return true;
}
catch(Exception)
{
MessageBox.Show("Incorrect code :(");
return false;
}
有人知道我如何删除输出文件吗?