Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

The process cannot access the file because it is being used by another process

$
0
0

I have posted my problem under SQL server, but when I only got one answer I try to post it here, perhaps someone can help me Under this  thread.

I want to delete a file from the server, and run the script shown by starting it from another script by first transfering the file ID.   First I get the filename  from a table and save it in the variable «bilde». Then I close the variables :

      objRdr.Close();
      myConn.Close();
     Dispose();

When the script comes to the line:
System.IO.File.Delete(filePath),

the program abort with the message:

«The process cannot access the file because it is being used by another process.»

I can not solve my problen. I have closed the file before deleting it, and I delay the program to let the close commands work.
 
Can someone help me please. 

Tom Knardahl

Norway

 

<script language="C#" runat="server">

    void Page_load(object sender, EventArgs e)

    {

      // Set the index of the file to asession variable (from the calling script)

        Session["bildeID"] = Convert.ToInt32(Request.QueryString["bildeID"]);

        Session["Sti"] = "~/Kongsvinger/";

        Session["SQL"] = "Server       =.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Historiebase.mdf;Integrated Security=True;User  Instance=True";

        Session["ConString"] = "HistorieConnectionstring";

       // Finding the file name

       SqlConnection myConn = new SqlConnection(Session["SQL"].ToString());

        myConn.Open();

        SqlCommand objCmd;

        SqlDataReader objRdr;

        string bilde = "";

        objCmd = new SqlCommand("SELECT  bilde  FROM bilder WHERE bildeID = @bildeID", myConn);

        objCmd.Parameters.Add("@bildeID", SqlDbType.VarChar).Value = Session["bildeID"];

        objRdr = objCmd.ExecuteReader();

        while (objRdr.Read())

        {

            bilde = Convert.ToString(objRdr["bilde"]);

       }

 // Set the filename to the session variable

       Session["Bilde"] = bilde;

    // Close the reader and the conn   

        objRdr.Close();

        myConn.Close();

        Dispose();

 //   Delete the file from the server

        string filePath = Server.MapPath(Session["Sti"] + "/bilder/" + bilde);

        if (System.IO.File.Exists(filePath))

             //  System.Threading.Thread.Sleep(30000);

           System.IO.File.Delete(filePath);        //  Here the abort occur

  } // End of Page_load

</script>


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>