Click here to Skip to main content
16,018,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I upload a .xlsx file to server folder and fetching data from this file , In the end i want to delete this file but once i am trying it raise exception "
Quote:
The action can't be completed because the file is open in IIS Express Worker Process
" I already close the connection after that it not getting delete.
Please help me how i can do it , how release it from iis.
my code is:
Quote:
OleDbConnection con = null;
string[] filePaths = null;

Quote:
DataSet ds1 = new DataSet();
filePaths = Directory.GetFiles(Server.MapPath("~/UploadInfo/"));
List<listitem> files = new List<listitem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
fileName = files[0].Text;
fullpath = files[0].Value;
string[] ext = fileName.Split('.');
if (ext[1] == "xls")
{
ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fullpath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (ext[1] == "xlsx")
{
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullpath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
string query = "SELECT * FROM [Sheet1$]";
con = new OleDbConnection(ConStr);
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

if (con.State == ConnectionState.Closed)
{
con.Open();
}


Quote:
con.Close();

String mappath = Server.MapPath("~//UploadInfo//" + fileName);
FileInfo file = new FileInfo(mappath);
if (file.Exists)
{
file.Delete(); // Here is the issue
}

Many thanx in advance
Posted
Updated 1-Dec-15 19:27pm
v2
Comments
phil.o 2-Dec-15 1:19am    
Please show the code where you handle the file in server side.
But not in a reply to my comment: use the "Improve question" button just above and qualify your question with relevant details.
Kornfeld Eliyahu Peter 2-Dec-15 1:59am    
The flow of your code is not clear from the sample, but it may the problem of a still opened connection created by the data adapter or explicitly...
Close all the explicit connections and destroy data adapter, command and table too...

1 solution

Put a using block around the construction of the Connection, Command and Adapter objects:
C#
using (con = new OleDbConnection(ConStr))
   {
   using (OleDbCommand cmd = new OleDbCommand(query, con))
      {
      ...
And only try to delete the file outside the outer using block - that way, all open activities are Disposed before you try, and it should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900