01-02-2011, 03:46 AM
Note that the code presented will work in versions of Visual Studio prior to VS2008 but will need to be altered as there are things like language extensions used in the code below which were new to Visual Studio as of VS2008 but the underlying code can and does work with VS2005, not sure about VS2003.
I have attached a demo VS2008 project.
There are times when developers need a temporary file created to do some work during the execution of a task followed by removing the file once the task has been completed. If the program crashes before you clean code (code to remove the temp file) runs to remove the file then it will be left behind. I know several developers this has happened to and ended up having to write code to get these files and remove them. The code presented below demonstrates how to mark a temporary file as remove when the application closes or you close the FileStream (wrapped in a language extension), which created the file. For precautions, it might be worth giving the file created by the FileStream a unique name so in the code the file name a random name using a language extension.
Create a button on a form called cmdCreateAndRemoveTempFileOnClose and place the following code into the click event
Create a code module and place the following into it
In the form you placed the first button add another button and call it cmdCreateAndRemoveTempFileOnClose2 with the code below for the click event
The second button code above removes the temp file when the FileStream is closed
Add another code module for my My Namespace addition for a message box wrapper for asking users a question (I have many more but this is all that is needed for the demo).
File contents
I have attached a demo VS2008 project.
There are times when developers need a temporary file created to do some work during the execution of a task followed by removing the file once the task has been completed. If the program crashes before you clean code (code to remove the temp file) runs to remove the file then it will be left behind. I know several developers this has happened to and ended up having to write code to get these files and remove them. The code presented below demonstrates how to mark a temporary file as remove when the application closes or you close the FileStream (wrapped in a language extension), which created the file. For precautions, it might be worth giving the file created by the FileStream a unique name so in the code the file name a random name using a language extension.
Create a button on a form called cmdCreateAndRemoveTempFileOnClose and place the following code into the click event
Hidden Content
Create a code module and place the following into it
Hidden Content
In the form you placed the first button add another button and call it cmdCreateAndRemoveTempFileOnClose2 with the code below for the click event
Hidden Content
The second button code above removes the temp file when the FileStream is closed
Add another code module for my My Namespace addition for a message box wrapper for asking users a question (I have many more but this is all that is needed for the demo).
File contents
Hidden Content