Prabu_Spark进程无法访问文件'uploaddoc.xls',因为它正被另一个进程使用 [英] Prabu_Spark the process cannot access the file 'uploaddoc.xls' because it is being used by another process

查看:97
本文介绍了Prabu_Spark进程无法访问文件'uploaddoc.xls',因为它正被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨先生,

我上传一个excel文件并从该excel文件中获取记录并存储在数据库中。完成将记录存储在数据库中后,我需要删除存储在上传文件夹中的文件。如果我尝试删除该文件,则会显示错误,例如进程无法访问文件'uploaddoc.xls',因为它正被另一个进程使用。我分享了代码。请给我解决这个问题的方法。



Hi sir,
I upload one excel file and get the records from that excel file and stored in database. After finish storing the records in the database,
i need to delete the file stored in "uploaded" folder. If i try to delete the file it shows error like "the process cannot access the file 'uploaddoc.xls' because it is being used by another process". I shared the code. Kindly give me the solution for this problem.

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        //System.Threading.Thread.Sleep(5000);

        if (FileUpload1.HasFile)
        {
            string filename = FileUpload1.FileName;

            string checking_ext = Path.GetExtension(filename);

            if (checking_ext == ".xls")
            {
                string file_location = Server.MapPath("uploads/" + filename);
                string count;
                FileUpload1.SaveAs(file_location);
                import_excel(file_location, out count);
                lbl_count.Text = count;
            }
            else
            {
                lbl_error.Text = "Please select the excel file";
            }
        }
        else
        {
            lbl_error.Text = "Please select the file";
        }
    }
    catch (Exception ex)
    {
        ErrorHandler.WriteError(ex.Message.ToString());
    }
    
}

private void import_excel(string path, out string cnt)
{
    try
    {
        OleDbConnection con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");
        OleDbCommand cmd;
        con.Open();
        cmd = new OleDbCommand("select * from [Sheet1$]", con);

        DataSet ds = new DataSet();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        oda.SelectCommand = cmd;
        oda.Fill(ds);       

        Session["total_count"] = cnt;
        Response.Redirect("Process1.aspx");

        if (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }
    }
    catch (Exception ex)
    {
        if (ex.Message == "External table is not in the expected format.")
        {
            lbl_error.Visible = true;
            lbl_error.Text = "Invalid File Format";            
        }
        else if (ex.Message == "'Sheet1$' is not a valid name.  Make sure that it does not include invalid characters or punctuation and that it is not too long." || ex.Message == "No value given for one or more required parameters.")
        {
            lbl_error.Visible = true;
            lbl_error.Text = "Invalid Excel Sheet";
            
        }
        cnt = "";

        ErrorHandler.WriteError(ex.Message.ToString());        
    }
}

推荐答案

,con);

DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter();
oda.SelectCommand = cmd;
oda.Fill(ds);

会话[ total_count] = cnt;
Response.Redirect( Process1。 aspx);

if (System.IO.File.Exists(path))
{
System.IO.File.Delete(路径);
}
}
catch (例外情况)
{
if (ex.Message == 外部表格不是预期的格式。
{
lbl_error.Visible = true ;
lbl_error.Text = 无效的文件格式;
}
else if (ex.Message == 'Sheet1
", con); DataSet ds = new DataSet(); OleDbDataAdapter oda = new OleDbDataAdapter(); oda.SelectCommand = cmd; oda.Fill(ds); Session["total_count"] = cnt; Response.Redirect("Process1.aspx"); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } catch (Exception ex) { if (ex.Message == "External table is not in the expected format.") { lbl_error.Visible = true; lbl_error.Text = "Invalid File Format"; } else if (ex.Message == "'Sheet1


'不是有效名称。请确保它不包含无效字符或标点符号并且它不会太长。 || ex.Message == 没有给出值一个或多个必需参数。
{
lbl_error.Visible = true ;
lbl_error.Text = 无效的Excel表格;

}
cnt = ;

ErrorHandler.WriteError(ex.Message.ToString());
}
}
' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long." || ex.Message == "No value given for one or more required parameters.") { lbl_error.Visible = true; lbl_error.Text = "Invalid Excel Sheet"; } cnt = ""; ErrorHandler.WriteError(ex.Message.ToString()); } }


con.Open();





您从Excel文件打开连接。



您应首先关闭连接,然后尝试删除文件。



you have open connection from Excel file.

You should first close the connection then try to delete file.


这篇关于Prabu_Spark进程无法访问文件'uploaddoc.xls',因为它正被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆