Bahman
Bahman

Reputation: 11

Temp file inside project solution?

Is there any way to have temp file inside a temp directory in c# solution and write the bytes into that and after finishing our process delete it without showing that file to users ?

Upvotes: 0

Views: 336

Answers (2)

Jakob Möllås
Jakob Möllås

Reputation: 4369

using System.IO;

class Logger
{
    static void Main()
    {
        // Create (and open) a new temporary file
        var filename = Path.GetTempFileName();
        var file = File.Open(filename, FileMode.Append);

        // Write bytes to it during the programs lifetime...
        file.Write(new byte[] { 1, 2, 3 }, 0, 3);

        // Just before closing your program, close and delete it
        file.Close();
        File.Delete(filename);
    }
}

Upvotes: 1

Paul Creasey
Paul Creasey

Reputation: 28844

consider using

string tempFile = Path.GetTempFileName();
//Use the file
File.Delete(tempFile);

Upvotes: 2

Related Questions