Link to home
Start Free TrialLog in
Avatar of Abhishek Modak
Abhishek ModakFlag for India

asked on

I have encountered an error while running my program on visual studio 2015

I want to make a program that encrypts and decrypts a file selected from a directory but i am getting an exception error
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\Users\HP\Desktop\AI Practical\AI_P4.txt' because it is being used by another process.
here is my code for encrypting the file
 private void EncryptFile(string sourceFile, string targetFile, string key)
        {
           
                AesManaged AES = new AesManaged();
                using (MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider())
                {
                    AES.KeySize = MD5.HashSize;
                    AES.BlockSize = MD5.HashSize;
                    AES.IV = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(_IV));
                    AES.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                }
                using (FileStream reader = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
                {
                    using (FileStream writer = new FileStream(targetFile, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (CryptoStream cs = new CryptoStream(writer, AES.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            int bufferSize = 4096;
                            byte[] buffer = new byte[bufferSize];
                            int bytesRead;
                            do
                            {
                                bytesRead = reader.Read(buffer, 0, bufferSize);
                                if (bytesRead != 0)
                                {
                                    cs.Write(buffer, 0, bufferSize);
                                }
                            }
                            while (bytesRead != 0);
                            cs.FlushFinalBlock();
                        }
                    }
                }
       }
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

getting an error below:

The name '_IV' does not exist in the current context

can you tell us how you declare _IV ?
Is C:\Users\HP\Desktop\AI Practical\AI_P4.txt the source or the target file?  (I guess target file)
You have this file opened and in use by something else hence this error message.  eg. you have the file opened in a text editor.

To solve it you need to stop the other process using the file.  A reboot would do that but that is overkill.
since C:\Users\HP\Desktop\AI Practical\AI_P4.txt is just a text file and if only your C# program is writing to it ( i guess it was the target file as well that being locked by other process), then it would be a running process in your C# program that accessing and then not closing that file properly.
you should edit this question and continue with this question since experts had started putting their comments here.

you can request to delete another one at:
https://www.experts-exchange.com/questions/29069996/I-have-encountered-an-error-while-running-my-program-on-visual-studio-2015.html
I don't see any more relevant information in the other question.
You have been told here what is wrong and how to cure it.  All that needs to be done is you to find which other process has the file open.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Abhishek Modak

ASKER

Thanks I have changed the line as suggested by you and it worked.  Thanks sir for your advice.
ps.  One day ago I did ask the following question:  Is C:\Users\HP\Desktop\AI Practical\AI_P4.txt the source or the target file?  If you had bothered checking and answering then you would have got this sorted out shortly afterwards.