开发者

Deleting file in asp.net using c#?

When I save a document file in my solution explorer, I send that doc file through mail then I am wanting to delete the document file. It is giving an error like this: process being used by another process.

Below please find my code:

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.Fi开发者_JAVA技巧leName);
                        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();

        }
    }


The most likely cause is the stream you created from

ResumeUpload.PostedFile.SaveAs

hasn't been closed. You could try to force it by disposing or closing the stream. HttpPostedFile has an InputStream property you can use for this:

InputStream
Gets a Stream object that points to an uploaded file to prepare for reading the contents of the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜