Thursday, 22 August 2013

The process cannot access the file

The process cannot access the file

Please have a look at the following code
void InformationWriter::writeContacts(System::String ^phone,
System::String ^email)
{
try
{
//Write the file
StreamWriter ^originalTextWriter = gcnew
StreamWriter("contacts.dat",false);
originalTextWriter->WriteLine(phone);
originalTextWriter->WriteLine(email);
originalTextWriter->Close();
//Encrypt the file
FileStream ^fileWriter = gcnew
FileStream("contacts.dat",FileMode::OpenOrCreate,FileAccess::Write);
DESCryptoServiceProvider ^crypto = gcnew DESCryptoServiceProvider();
crypto->Key = ASCIIEncoding::ASCII->GetBytes("Intru235");
crypto->IV = ASCIIEncoding::ASCII->GetBytes("Intru235");
CryptoStream ^cStream = gcnew
CryptoStream(fileWriter,crypto->CreateEncryptor(),CryptoStreamMode::Write);
//array<System::Byte>^ phoneBytes =
ASCIIEncoding::ASCII->GetBytes(phone);
FileStream ^input = gcnew
FileStream("contacts.dat",FileMode::Open); //Open the file to be
encrypted
int data = 0;
while((data=input->ReadByte()!=-1))
{
cStream->WriteByte((System::Byte)data);
}
input->Close();
cStream->Close();
fileWriter->Close();
System::Windows::Forms::MessageBox::Show("Data Saved");
}
catch (IOException ^e)
{
System::Windows::Forms::MessageBox::Show(e->Message);
}
}
When the following part get executed, I get an error
FileStream ^input = gcnew FileStream("contacts.dat",FileMode::Open);
//Open the file to be encrypted
Below is the error I get

This is the first time I am using CryptoStream and I new to C++/CLI as well.
Please help

No comments:

Post a Comment