Recipe 12.21. Creating a Temporary File


Problem

You need to quickly store some data in a file, but the data will just be around for a little while.

Solution

Create a temporary file in the user's "official" temporary file area with the My.Computer. FileSystem.GetTempFileName() method:

 Dim workFile As String = _    My.Computer.FileSystem.  GetTempFileName( ) 

The filename returned represents a brand-new file created by the method that is zero bytes in size. When you are ready to use it, open it with one of the stream-based or file handle-based file-management methods, and make any additions or changes as needed. When you are finished, simply delete the file.

Discussion

The temporary file is added to the user's default temporary file area and always has a .tmp extension. The filename is guaranteed to be unique and will not conflict with other temporary filenames stored in that same directory. The typical location for a logged-in Windows user is:

 C:\Documents and Settings\<user>\Local Settings\Temp\ 

See Also

Recipe 12.16 discusses the editing of files using either stream-based or file handle-based methods.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net