I need to create a temporary file. I'd like to save it in the temp folder, but I'm not sure what's the best way of getting the full path of this folder.
If I use Sitecore.Configuration.Settings.TempFolderPath
it only gives me the folder name, not the full path.
Is there any helper or similar that gives me this information?
-
1Aren't you just looking for MapPath (combined with TempFolderPath)? msdn.microsoft.com/en-us/library/…– GatogordoCommented Aug 1, 2017 at 9:25
2 Answers
For a temporary file
Actually, Sitecore itself doesn't use TempFolderPath when it needs to create a temporary file.
Looking inside the Sitecore PackageManager classes, Sitecore does this when it needs a temporary file.
string tempFileName = System.IO.Path.GetTempFileName();
Using TempFolderPath
The thing about this setting is; it can be relative to the current web root folder, but it could also be defined as an absolute path (outside your webroot) in which case a MapPath call could fail.
Sitecore has a utility class that deals with this. You can find it in FileUtil, so your code would look like this:
string tempFolder = Sitecore.IO.FileUtil.MapPath(Sitecore.Configuration.Settings.TempFolderPath);
This snippet would work, with both relative and absolute path definitions.
you can use :
string tempFullPath = Sitecore.MainUtil.MapPath(Settings.TempFolderPath);
or
string tempFullPath = Sitecore.IO.FileUtil.MapPath(Settings.TempFolderPath);