Creating a Temporary Directory in Windows

Creating a temporary directory in Windows?

No, there is no equivalent to mkdtemp. The best option is to use a combination of GetTempPath and GetRandomFileName.

You would need code similar to this:

public string GetTemporaryDirectory()
{
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);
return tempDirectory;
}

How to create temporary folder in local system using c#

This will create a temporary directory:

   string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDirectory);

Create a temporary directory in PowerShell?

I think it can be done without looping by using a GUID for the directory name:

function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}

Original Attempt With GetRandomFileName

Here's my port of this C# solution:

function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}

Analysis Of Possibility Of Collision

How likely is it that GetRandomFileName will return a name that already exists in the temp folder?

  • Filenames are returned in the form XXXXXXXX.XXX where X can be either a lowercase letter or digit.
  • That gives us 36^11 combinations, which in bits is around 2^56
  • Invoking the birthday paradox, we'd expect a collision once we got to around 2^28 items in the folder, which is about 360 million
  • NTFS supports about 2^32 items in a folder, so it is possible to get a collision using GetRandomFileName

NewGuid on the other hand can be one of 2^122 possibilities, making collisions all but impossible.

Creating a unique temporary directory from pure C in windows

You can use what GetTempPath returns concatenated with a Guid to ensure uniqueness of the directory. You can create a Guid using UuidCreate or CoCreateGuid Function.

To delete recursively the directory, there is an example here in pure C: How to remove directory recursively? based on FindFirstFile, FindNextFile, DeleteFile and RemoveDirectory.

There is also SHFileOperation but it's more heavyweight and is based on the Windows Shell functions, and the Shell DLLs are not always wanted, especially if you're writing server code.

Create temporary file in C, MS Windows system

GetTempPath

Retrieves the path of the directory designated for temporary files.

GetTempFileName

Creates a name for a temporary file. If a unique file name is
generated, an empty file is created and the handle to it is released;
otherwise, only a file name is generated.

These two provide you easy way to obtain a location and name for a temporary file.

UPD: Code sample on MSDN: Creating and Using a Temporary File.

Creating A Folder In the Temp Folder

With CreateDir() You must create dirs one after the other and not a dir structure at once.

if not DirExists(ExpandConstant('{tmp}\Utilities')) then
CreateDir(ExpandConstant('{tmp}\Utilities'));
if not DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
CreateDir(ExpandConstant('{tmp}\Utilities\SDK'));

if DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
Log('Temp\Utilities\SDK Folder Has Been Created.') else
Log('Temp\Utilities\SDK Folder ERROR : NOT Created.');

Inno Setup has a function to create a dir structure at once
function ForceDirectories(Dir: string): Boolean;

Example:

if not DirExists(ExpandConstant('{tmp}\Utilities\SDK')) then
ForceDirectories(ExpandConstant('{tmp}\Utilities\SDK'));

Also keep in mind :

  • {tmp} all is related to the Inno Setup Temp folder is-XXXXX.tmp
    C:\Users\...\AppData\Local\Temp\is-XXXXX.tmp
  • {%temp} is the users Temp folder

    C:\Users\...\AppData\Local\Temp

Creating temporary folders

Update: Added File.Exists check per comment (2012-Jun-19)

Here's what I've used in VB.NET. Essentially the same as presented, except I usually didn't want to create the folder immediately.

The advantage to use GetRandomFilename is that it doesn't create a file, so you don't have to clean up if your using the name for something other than a file. Like using it for folder name.

Private Function GetTempFolder() As String
Dim folder As String = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
Do While Directory.Exists(folder) or File.Exists(folder)
folder = Path.Combine(Path.GetTempPath, Path.GetRandomFileName)
Loop

Return folder
End Function

Random Filename Example:

C:\Documents and Settings\username\Local Settings\Temp\u3z5e0co.tvq


Here's a variation using a Guid to get the temp folder name.

Private Function GetTempFolderGuid() As String
Dim folder As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Do While Directory.Exists(folder) or File.Exists(folder)
folder = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Loop

Return folder
End Function

guid Example:

C:\Documents and Settings\username\Local Settings\Temp\2dbc6db7-2d45-4b75-b27f-0bd492c60496



Related Topics



Leave a reply



Submit