Click here to Skip to main content
15,896,514 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using text file for some processes in my application.When i am closing my application the text file also need to be closed.How to do that?Kindly tell me your suggestions.

Thank you,
Posted

There is Form_Closing event. Used that to delete your txt file.
 
Share this answer
 
Comments
sreenathpktr 4-Nov-11 8:32am    
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{

}

I have tried this event but it's not working.
To delete the file, use File.Delete(string filepath) method.
 
Share this answer
 

You can use the Closed event, for that purpose:


If you have a FileStream and you want to close it when the form is closed, you can add an event-handler to the Closed event using the designer or, in the constructor of your form, like the following:


C#
this.Closed += new EventHandler(Form1_Closed);

and implement the event-handler to close your file, like the following:


C#
void Form1_Closed(object sender, EventArgs e)
{
    myFileStream.Close();
}
 
Share this answer
 
v2
Comments
sreenathpktr 4-Nov-11 9:12am    
Thank you very much...This is very helpful..
BobJanova 4-Nov-11 11:50am    
If it's the main form, bad practice though it may be, the file handle will be closed when the process exits anyway.

However, you may well have hit upon why attempting to delete the file is failing.
You can use File.Delete to delete the file on FormClosing[^] as Prasad said.

http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx[^]
 
Share this answer
 
use this code but make sure that the file is not used by any application
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  File.Delete( path + filename);
}
 
Share this answer
 
v2
If, as Shmuel Zang suggests, you have a handle to this file (i.e. a FileStream, StreamWriter, TextWriter etc) open, you won't be able to delete it until after you close the handle. So in the Closed event you should do something like

FileStream fs; // let's say you have this
// if it's a StreamWriter etc the code is basically the same

void FormClosed(object sender, EventArgs e){
 fs.Close();
 
 File.Delete(filename);
}


Note that (contrary to some of the solutions above) you should use the FormClosed event for terminal tidy-up tasks like this, not FormClosing. FormClosing is called when a close request is received (i.e. the user presses Alt+F4, or code calls Close), and any handler attached to the form can reject it and cause the form not to be closed after all. FormClosed is called when the form has actually been closed, and there is no chance it won't be closed. That's the point where you want to clear things up which you've created during the course of the application. (Well, either there or in your Main method after the Application.Run.)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900