Surya sasidhar
Surya sasidhar

Reputation: 30303

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.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();

        }
    }

Upvotes: 0

Views: 1434

Answers (1)

Chris S
Chris S

Reputation: 65426

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.

Upvotes: 2

Related Questions