Posted by: Cirilo Meggiolaro | 04/7/2009

Tip of the day #175 – Temp files easily

There are three useful static methods for those that need to work with temporary files. All methods are available under the System.IO.Path class;

GetTempFileName

  • The GetTempFileName method creates a zero-byte file with the extension .tmp in the local settings folder for the user and retrieves the full path as string:

Console.WriteLine(Path.GetTempFileName());
// Output: C:\Documents and Settings\Cirilo\Local Settings\Temp\tmp302.tmp

 
GetRandomFileName

  • The GetRandomFileName method returns a string that may be used as the name of a temporary folder or file.

Console.WriteLine(Path.GetRandomFileName());
// Output: 3iojffds.rv0

 
GetTempPath

  • The GetTempPath method returns a string with the path of the local settings temporary folder.

Console.WriteLine(Path.GetTempPath());
// Output: C:\Documents and Settings\Cirilo\Local Settings\Temp\

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: