使用c#删除asp.net中的文件?

时间:2010-01-18 11:26:38

标签: c# asp.net

当我在解决方案资源管理器中保存文档文件时,我通过邮件发送该doc文件,然后我想删除该文档文件。它给出了这样的错误:process being used by another process

以下是我的代码:

protected void btnsubmit_Click(object sender, EventArgs e)
    {           
        if (Label1.Text == txtverifytxt.Text)
        {
            if (rdoSevice.SelectedItem.Value == "1")
            {
                PackageType = ddlindPackages.SelectedItem.Text;
            }
            else if (rdoSevice.SelectedItem.Value == "2")
            {
                PackageType = ddlCorpPack.SelectedItem.Text;
            }
            if (ResumeUpload.PostedFile != null)
            {

                HttpPostedFile ulFile = ResumeUpload.PostedFile;
                string file = ulFile.FileName.ToString();
                FileInfo fi = new FileInfo(file);

                string ext = fi.Extension.ToUpper();
                if (ext == ".DOC" || ext == ".DOCX")
                {
                    int nFileLen = ulFile.ContentLength;
                    if (nFileLen > 0)
                    {
                        strFileName = Path.GetFileName(ResumeUpload.PostedFile.FileName);
                        strFileName = Page.MapPath("") + "\\Attachments\\" + strFileName;
                        ResumeUpload.PostedFile.SaveAs(strFileName);
                    }
                    sendingmail();
                    FileInfo fi1 = new FileInfo(strFileName);
                    ResumeUpload.FileContent.Dispose();
                    Label2.Visible = true;
                    Label2.Text = "Request sent sucessfully";
                    fi1.Delete();
                    //if (File.Exists(strFileName))
                    //{
                    //    File.Delete(strFileName);
                    //}
                    ClearAll(tblOrdernow);
                    //Response.Redirect("CheckOut.aspx");
                }
                else
                {
                    Label2.Visible = true;
                    Label2.Text = "Upload only word documents..";
                }
            }
            else
            {
                Label2.Visible = true;
                Label2.Text = "Do not upload empty document..";
            }
        }
        else
        {

            Label2.Visible = true;
            Label2.Text = "Verify Image not Matched";
            Label1.Text = ran();

        }
    }

1 个答案:

答案 0 :(得分:2)

最可能的原因是您从

创建的流
ResumeUpload.PostedFile.SaveAs

尚未关闭。您可以尝试通过处理或关闭流来强制它。 HttpPostedFile有一个可用于此的InputStream属性:

  

<强>的InputStream
  获取一个Stream对象   指向要准备的上传文件   用于读取文件的内容。