Solved

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

Posted on 2008-10-10
5
558 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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