0

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

        }
    }

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Mr.Chris S ResumeUpload.PostedFile i get this only, i not get dispose or close i tried this thanks for response
sorry Mr.Chris S i not under stand
ResumeUpload.PostedFile.InputStream.Close is my suggestion, but I haven't tried it
Thank you Mr. Chris S it is working fine thank u for response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.