Temp Files Asp.net

user2013107 picture user2013107 · Feb 25, 2013 · Viewed 7.3k times · Source

Is it possilbe to create temp files and images in asp.net applications using something like this:

If no, how can i do it? (ImagePB is a previously treated Bitmap)

if (System.IO.File.Exists(System.IO.Path.GetTempPath() + @"img" + imgID.ToString() + "PB" + extencao) == true)
{
    try
    {
        System.IO.File.Delete(System.IO.Path.GetTempPath() + @"img" + imgID.ToString() + "PB" + extencao);
        imagePB.Save(System.IO.Path.GetTempPath() + @"img" + imgID.ToString() + "PB" + extencao, imgFormat);
    }
    catch (Exception)
    { }
}

Answer

Steven V picture Steven V · Feb 25, 2013

Yes, GetTempPath() should return a temporary file path, and the code you have posted should work. http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx has more information about how GetTempPath() get's the path.

Though, it does not verify if the Temp Path directory exists, or is writeable by the application. I haven't run into a situation where GetTempPath() does return an inaccessible path. You'd probably want to account for this in your application to handle this situation.

Also be mindful, this is very possibly C:\Windows\Temp. It could have limited disk space, or deleted at any time by someone else when disk space is needed. You may want to create a temp path within your application, and delete it when you no longer need it.