Search code examples
.nettemporary-files

Temp Filename on specific partition?


I know about Path.GetTempFileName() and how to get the temp folder (usually its on your C drive)

But how do i get a temp filename on a specific partition? i think as a workaround i'll do something like targetBaseDir/temp.tmp and then File.Move when its complete.


Solution

  • Why not just create your own GetTempFilePath method?

    Something like this

    string GetTempFilePath(string basePath, string extension)
    {
        return Path.Combine(basePath, Guid.NewGuid().ToString()+"."+extension);
    }
    
    //Usage
    GetTempFilePath("E:\\", "tmp");
    
    //Output
    //E:\e2e4873e-daf5-41b6-bdc5-2afec61921e2.tmp
    

    Or you can use the native GetTempFileName method that is used by System.IO.Path.GetTempFileName()