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;
                                bytesRead = reader.Read(buffer, 0, bufferSize);
                                if (bytesRead != 0)
                                    cs.Write(buffer, 0, bufferSize);
                            while (bytesRead != 0);
Abhishek ModakStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
getting an error below:

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

can you tell us how you declare _IV ?
AndyAinscowFreelance programmer / ConsultantCommented:
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.
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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:
AndyAinscowFreelance programmer / ConsultantCommented:
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.
Chris StanyonWebDevCommented:
Looking at your other question, you have this code:

private void btn_Encrypt_Click(object sender, EventArgs e)
    OpenFileDialog open = new OpenFileDialog();
    txtLocalEncFile.Text = open.FileName;
    SaveFileDialog save = new SaveFileDialog();
    txtNewEncFile.Text = open.FileName;
    EncryptFile(txtLocalEncFile.Text, txtNewEncFile.Text, key);

Open in new window

Pay particular attention to the 3rd and 6th lines - you're setting the Local File and New File to the same file, so by the time you call the Encrypt method, you open the file to read and then try to write to the same file - which is obviously already open! I'm guessing line 6 should really be:

txtNewEncFile.Text = save.FileName;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abhishek ModakStudentAuthor Commented:
Thanks I have changed the line as suggested by you and it worked.  Thanks sir for your advice.
AndyAinscowFreelance programmer / ConsultantCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.