When running applications, there is often a need to create temporary files and open them in your app or in a 3rd party app. In the end, the file(s) should be removed from disk on custom events or on application exit at latest (if possible, still existent and not opened/blocked in/by 3rd party app)
using (var f = new CompuMaster.IO.TemporaryFile(".txt"))
{
System.IO.File.WriteAllBytes(f.FilePath, new Byte[] { });
Console.WriteLine("some work");
}
// if file is not locked by another task: file is already removed
// if file is locked by another task: file will be removed on final call (see usage pattern below)
// CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit();
'
Using f As New CompuMaster.IO.TemporaryFile(".txt"))
System.IO.File.WriteAllBytes(f.FilePath, New Byte() {})
Console.WriteLine("some more work")
End Using
'if file is not locked by another task: file is already removed
'if file is locked by another task: file will be removed on final call (see usage pattern below)
' CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit()
static void Main(string[] args)
{
try
{
var f = New CompuMaster.IO.TemporaryFile(".txt")
//do something more...
}
finally
{
CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit();
}
}
Sub Main(args as String())
Try
Dim f As New CompuMaster.IO.TemporaryFile(".txt")
'do something more...
Finally
CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit()
End Try
End Sub
void MainForm_FormClosing(Object sender, FormClosingEventArgs e)
{
CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit();
}
Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
CompuMaster.IO.TemporaryFile.CleanupOnApplicationExit()
End Sub