Solved

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

Posted on 2008-10-10
5
557 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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