On The Insider: Justin Timberlake Hosts the ESPYs

Create a temp file with VB.NET

Tags: Microsoft development tools, Programming languages, Irina Medvinskaya, temp file, Microsoft Visual Basic.Net, Visual Basic Tips Newsletter

  • Save
  • Print
  • Digg This
  • 1

Takeaway: Irina Medvinskaya looks at the VB.NET code that allows you to get the name of the file that you can use as a temp file. She also provides sample code that shows how to create a temp file.

Temp files are generally used for temporary storage and data manipulation. This is often necessary for storing user data, user preferences, session information, application cache, and many other types of information.

In order to get the name of the file that you can use as a temp file in VB.NET, I suggest using the GetTempFileName method of the Path class. While there are multiple approaches to creating a temp file, it's really helpful to use the System.IO.Path because it returns a unique file name in the current user's temporary directory; you can use that file name to create a file for storing temporary information. Note that calling this method multiple times will result in getting a different file name each time even if you don't use that name to create a file. This behavior prevents name collisions between multiple applications.

In my sample code in Listing A, I define the string variable, sTempFileName, and assign the System.IO.Path.GetTempFileName method's return value to it. This produces a temp file name that I can use. I then create a FileStream object, fsTemp, and request that the system create a file with the filename, sTempFileName. Once the file is created, I add data to it (this code is omitted in the example). After that, the temp file is closed.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

  • Save
  • Print
  • Digg This
  • 1

Print/View all Posts Comments on this article

Lifespan of Filejetter  | 05/18/07
Lifespan of filetct  | 05/18/07

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

advertisement
Click Here