Creating temporary folders

2019-01-11 00:57发布

I am working on a program that needs to create a multiple temporary folders for the application. These will not be seen by the user. The app is written in VB.net. I can think of a few ways to do it such as incremental folder name or random numbered folder names, but I was wondering, how other people solve this problem?

标签: .net io
13条回答
混吃等死
2楼-- · 2019-01-11 01:20

As long as the name of the folder doesn't need to be meaningful, how about using a GUID for them?

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-11 01:21

Something like...

using System.IO;

string path = Path.GetTempPath() + Path.GetRandomFileName();
while (Directory.Exists(path))
 path = Path.GetTempPath() + Path.GetRandomFileName();

Directory.CreateDirectory(path);
查看更多
甜甜的少女心
4楼-- · 2019-01-11 01:22

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

查看更多
Evening l夕情丶
5楼-- · 2019-01-11 01:22
Dim NewFolder = System.IO.Directory.CreateDirectory(IO.Path.Combine(IO.Path.GetTempPath, Guid.NewGuid.ToString))
查看更多
干净又极端
6楼-- · 2019-01-11 01:28

The advantage to using System.IO.Path.GetTempFileName is that it will be a file in the user's local (i.e., non-roaming) path. This is exactly where you would want it for permissions and security reasons.

查看更多
神经病院院长
7楼-- · 2019-01-11 01:30

There's a possible race condition when:

  • creating a temp file with GetTempFileName(), deleting it, and making a folder with the same name, or
  • using GetRandomFileName() or Guid.NewGuid.ToString to name a folder and creating the folder later

With GetTempFileName() after the delete occurs, another application could successfully create a temp file with the same name. The CreateDirectory() would then fail.

Similarly, between calling GetRandomFileName() and creating the directory another process could create a file or directory with the same name, again resulting in CreateDirectory() failing.

For most applications it's OK for a temp directory to fail due to a race condition. It's extremely rare after all. For them, these races can often be ignored.

In the Unix shell scripting world, creating temp files and directories in a safe race-free way is a big deal. Many machines have multiple (hostile) users -- think shared web host -- and many scripts and applications need to safely create temp files and directories in the shared /tmp directory. See Safely Creating Temporary Files in Shell Scripts for a discussion on how to safely create temp directories from shell scripts.

查看更多
登录 后发表回答