system.IO.IOException: the process cannot access the file


I need to upload csv file to database after an update made by it and the filename is the same each time. However, it throws the system.IO.IOException: the process cannot access the file

here is the code


Open in new window

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.

before you dispose, do you have to close anything?

can  you place a try/catch around code...

string str_Error;      
//close upfile here...
catch (Exception ex)
     str_Err = ex.StackTrace.ToString();   //gives BETTER ERROR message...
techquesAuthor Commented:
However, it works on localhost. When I upload the web application to remote machine, it happens that error after upload the file first time.

It means it is ok to upload in the first time.
It throws such exception in 2nd time.  
It's most likely because the filename is the same...if the file is not there, does it work?
techquesAuthor Commented:
System.IO.StreamReader strFile = new System.IO.StreamReader(Server.MapPath(@"csv\temp.csv"));

As I just need to read the data from the csv file and then insert to remote database.

Actually, there is no need to save the file.

However, how to handle this code?

Why not create a stream from the uploaded file without saving it to the disk. Try this

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
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
.NET Programming

From novice to tech pro — start learning today.