Solved

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

Posted on 2008-10-10
5
545 Views
Last Modified: 2013-12-17
Hi

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

upfile.SaveAs(Server.MapPath(@"csv\temp.csv")); 

upfile.Dispose();

Open in new window

0
Comment
Question by:techques
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 22688675
before you dispose, do you have to close anything?

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

string str_Error;      
try{
upfile.SaveAs(Server.MapPath(@"csv\temp.csv"));
//close upfile here...
}
catch (Exception ex)
{
     str_Err = ex.StackTrace.ToString();   //gives BETTER ERROR message...
}
finally
{
      upfile.Dispose();
}
0
 

Author Comment

by:techques
ID: 22688781
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.  
0
 
LVL 21

Expert Comment

by:silemone
ID: 22688848
It's most likely because the filename is the same...if the file is not there, does it work?
0
 

Author Comment

by:techques
ID: 22692491
upfile.SaveAs(Server.MapPath(@"csv\temp.csv"));
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?
System.IO.StreamReader(Server.MapPath(@"csv\temp.csv"));

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 50 total points
ID: 22698926
Why not create a stream from the uploaded file without saving it to the disk. Try this
strFile=upfile.postedfile.inputstream()
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now