简体   繁体   中英

Temp Files Asp.net

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)
    { }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM