Problem with file.delete()

when i try to delete some file the application throws error

the error is this:

<b>
System.IO.IOException:
The process cannot access the file 
"C:\Inetpub\wwwroot\LoadingMaps\MapBin\italy.jpg" 
because it being used by another process
</b>

and the code that cause this throw error is this:

<b>
private void button1_Click(object sender, System.EventArgs e)
{
string path = Directory.GetCurrentDirectory();
MessageBox.Show(path);

//

pctMap.Visible=false;
if (pctMap.Image != null) 
{
pctMap.Image.Dispose();
pctMap.Image = null;
}


//MessageBox.Show("you are going to delete this map");
//MessageBox.Show(cbxMaps.SelectedValue.ToString()+"  "+cbxMaps.Text.ToString());


destMapPath=destMapPath+cbxMaps.SelectedValue.ToString();
//MessageBox.Show(destMapPath);
try
{
//DELETING MAP FROM THE MAPS FOLDER
//=================================
File.Delete(destMapPath);               //--this line throw error

//DELETE MAP FROM DB
//=============================
sqlQuery="DELETE * from Maps WHERE FileName=  and MapName= ";
dbCommand=new OleDbCommand(sqlQuery,dbConnection);
dbCommand.Parameters.Add("@SelectedMapFileName",DbType.String).Value=cbxMaps.SelectedValue.ToString();
dbCommand.Parameters.Add("@NewMapName",DbType.String).Value=cbxMaps.Text.ToString();
try
{
dbConnection.Open();
rowsAffected = 0;
rowsAffected = dbCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
lblError.Visible=true;
lblError.Text=ex.ToString();
}
if(rowsAffected!=0)
{
lblError.Visible=true;
lblError.Text="     ";
dbConnection.Close();
}
else
{
lblError.Visible=true;
lblError.Text="problem with deleting this map";
}
LoadApplication();


}
catch(Exception ex)
{
lblError.Visible=true;
//lblError.Text="error deliting file";
lblError.Text=ex.ToString();
}

}
}
</b>

what is the problem


Answer this question

Problem with file.delete()

  • db9

    this tool not help me
    do you know how i can to solve this problem with vs.net

  • BobTheCorkDwarf

    I understand,
    I think that it is the  operation system locked the picture that i wont to delete'
    I forget to mention that the deleting problem occur remittently,
    once delete ,once throw error message

    the problem is that:

    i have a form that can also create new file with
    File.Copy();

    and also shold Delete those files that i created,
     File.Delete();

    The idea is that  i could create the file and still have the option to Delete this file
    immediately after i created it,
    I thing that after creating file  the Operation System locked the fle

    Is it exist some way to unlocked this file  

  • TAG275

    One taught do you have Index server turned on 
    If so I have seen this problem occur when index server is reading the file at the same time your trying to access it. Might check that.

  • kshay


    That error message usually indicates that the file is being used by another process -- maybe you have it open in an image viewer somewhere else when this code executes  

    If you get stuck on figuring out what it is, you might be interested in a tool by SysInternals called Handle[1]. You can use this tool to see what program has a lock on a file to see what is stopping you from deleting it. Process Explorer[2] will tell you the same, too, and can be used as a replacement for Windows Task Manager.

    Good luck,

    -Paul

    [1] http://www.sysinternals.com/ntw2k/freeware/handle.shtml
    [2] http://www.sysinternals.com/ntw2k/freeware/procexp.shtml

  • snehalppatel

    I understand ,thank you for these links,
    but my little  problem still make me crazy ,cause
    I tried a lot of ways to delete files without get error,
    but still without GC.Collect  i can not solve this,
    okay i am not despair,
    finally  i will find some way to solve this problem!

    thank you anyway!

  • Bigforky


    I think that you should generally avoid making calls to GC.Collect() -- this might indicate an open reference somewhere in your app that gets cleaned up when the collector runs. The best thing to do would be to track this down and release it yourself before you rely on calling the GC.Collect.

    Here's some additional readin gon GC.Collect:

    http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx

    and also:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconforcingcollection.asp


    -Paul

  • Jezebel


    After you copy the file using File.Copy, do you perform any additional actions on the file through your app  Maybe the app itself is locking the file and if that's the case, you'll need to look through your code to make sure you don't have any open references to it when you delete.

    It's really hard to say what might be the specific cause of your problem without seeing your code. Feel free to contact me offline if you need further help.

    -Paul

  • marabu

    I solve this probem with

    System.GC.Collect();

    just before File.Delete();

    Is this bad way to deal with this problem



  • Chen Hao


    Are you still having the problem  Sometimes you'll get a lock on the file (strangely enough) just by having VS.NET open. If you close and reopen VS.NET, this problem may go away. Otherwise, you have some other process on your system locking the file and you'll need to use either of the tools I suggested to track it down. VS.NET cannot tell you itself what program has your file locked.

    Good luck,

    -Paul

  • Arachne53

    No I do not have  Index server turned on ,

    What you thing obout my way solving this problem with GC.Collect();

  • Problem with file.delete()