Creating a Unique and Temporary Filenames in C#.

vishalneeraj-24503
Posted by vishalneeraj-24503 under C# category on | Points: 40 | Views : 1004
Sometimes we may want to create a temporary file.Instead of manipulating the user's temporary directory and then manually generating the random file name,we can use the GetTempFileName method defined in the Path class of System.IO namespace.

If we do not need to create the file,then we can use the Path.GetRandomFileName() instead.
Below is a sample code demonstrating how to create a temporary file and write a text to it.
Import the namespace as System.IO.

using System.IO;
private static void Main(string[] args)
{
string tempFileName = Path.GetTempFileName();
using (StreamWriter streamWriter = File.CreateText(tempFileName))
{
streamWriter.WriteLine("Welcome to Dotnetfunda.com");
}
Console.ReadLine();
}

Comments or Responses

Login to post response