更新时间:2023-11-23 08:38:34
我认为可以通过使用GUID作为目录名而无需循环来完成此操作:
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)
}
这是我的此C#解决方案的端口:
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
$name = [System.IO.Path]::GetRandomFileName()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
GetRandomFileName
返回临时文件夹中已经存在的名称的可能性有多大?
How likely is it that GetRandomFileName
will return a name that already exists in the temp folder?
XXXXXXXX.XXX
的形式返回,其中X可以是小写字母或数字.GetRandomFileName
XXXXXXXX.XXX
where X can be either a lowercase letter or digit.GetRandomFileName
NewGuid
可能是2 ^ 122种可能性之一,几乎不可能发生碰撞.
NewGuid
on the other hand can be one of 2^122 possibilities, making collisions all but impossible.