我正在制作一个小备份工具......我有一点问题,不知道如何解决这个问题。那就是我在这里问的原因......代码:
strDirectoryData = dlg1.SelectedPath;
strCheckBoxData = "true";
clsCrypto aes = new clsCrypto();
aes.IV = "MyIV"; // your IV
aes.KEY = "MyKey"; // your KEY
strDirectoryEncryptedData = aes.Encrypt(strDirectoryData, CipherMode.CBC);
strCheckBoxEncryptedData = aes.Encrypt(strCheckBoxData, CipherMode.CBC);
StreamWriter dirBackup = new StreamWriter(dirBackupPath, false, Encoding.UTF8);
StreamWriter checkBackup = new StreamWriter(autoBackupPath, false, Encoding.UTF8);
dirBackup.WriteLine(strDirectoryEncryptedData, Encoding.UTF8);
dirBackup.Close();
checkBackup.WriteLine(strCheckBoxData, Encoding.UTF8);
checkBackup.Close();'
每次都出错 - 进程无法访问该文件,因为它正由另一个进程使用...
我也在Form1_Load
中有这个if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
string strCheckBoxData;
string strDirectoryData;
string strCheckBoxEncryptedData;
string strDirectoryEncryptedData;
strDirectoryData = "Nothing here";
strCheckBoxData = "false";
clsCrypto aes = new clsCrypto();
aes.IV = "MyIV"; // your IV
aes.KEY = "MyKey"; // your KEY
strDirectoryEncryptedData = aes.Encrypt(strDirectoryData, CipherMode.CBC);
strCheckBoxEncryptedData = aes.Encrypt(strCheckBoxData, CipherMode.CBC);
StreamWriter dirBackup = new StreamWriter(dirBackupPath, false, Encoding.UTF8);
StreamWriter checkBackup = new StreamWriter(autoBackupPath, false, Encoding.UTF8);
dirBackup.WriteLine(strDirectoryEncryptedData);
dirBackup.Close();
checkBackup.WriteLine(strCheckBoxEncryptedData);
checkBackup.Close();
}
else
{
string strCheckBoxDecryptedData;
string strDirectoryDecryptedData;
StreamReader dirEncrypted = new StreamReader(dirBackupPath);
StreamReader checkEncrypted = new StreamReader(autoBackupPath);
有什么想法吗?
答案 0 :(得分:4)
您没有正确关闭资源。您无法打开要写入的文件,因为您已将其打开以进行阅读,但尚未将其关闭。
完成使用后,您需要处理StreamReader
个对象。 StreamReader
类实现IDisposable
。我建议您使用using
块,以便即使存在异常也始终关闭该文件。
using (StreamReader dirEncrypted = new StreamReader(dirBackupPath)) {
// read from dirEncrypted here
}
相关强>