-1

I'm making a little backup tool... And I've got a little problem, no idea how to fix this. So that why I'm asking here... CODE:

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();'

Getting a error every time - The process cannot access the file because it is being used by another process...

Also i have this in 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);

Any ideas?

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

4

You aren't closing your resources properly. You can't open the file for writing because you have opened it for reading but you have not closed it again.

You need to dispose of your StreamReader objects when you are done using them. The StreamReader class implements IDisposable. I suggest that you use a using block so that the file will always be closed even if there is an exception.

using (StreamReader dirEncrypted = new StreamReader(dirBackupPath)) {
     // read from dirEncrypted here
}

Related

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Changed `StreamWriter dirBackup = new StreamWriter(dirBackupPath, false, Encoding.UTF8); dirBackup.WriteLine(strDirectoryEncryptedData); dirBackup.Close(); ` into `using (StreamWriter dirBackup = new StreamWriter(dirBackupPath, false, Encoding.UTF8)) { //write line here }` and still gettin the error. – user1927274 Dec 24 '12 at 20:35
  • @user1927274: Whilst I think the change you propose is a *very* good idea and you absolutely *should* do that, I don't think it's related to the issue that's giving you your current problem. Please read my answer again - it refers to the **`StreamReader`**, not the `StreamWriter`. – Mark Byers Dec 24 '12 at 20:40
  • but I get error in **`StreamWriter`** not the `StreamReader`.. `StreamReader` can't get any errors because its in current time isn't in use. – user1927274 Dec 24 '12 at 20:43
  • Oh wait.. I've got.. `StreamReader` opens the file, and thats why `StreamWriter` can't get access... Thanks! – user1927274 Dec 24 '12 at 20:46