Create a temp file and delete it in CSharp

Description

The following code shows how to create a temp file and delete it.

Example


         //from   w  w  w  .  j  a  va 2s .  c om

using System;
using System.IO;

static class MainClass
{
    static void Main()
    {
        string tempFile = Path.GetTempFileName();

        Console.WriteLine("Using " + tempFile);

        using (FileStream fs = new FileStream(tempFile, FileMode.Open))
        {
            // (Write some data.)
        }

        File.Delete(tempFile);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip