我的工作,需要为应用程序创建多个临时文件夹的程序。这些将不会被用户看到。该应用程序是用VB。我能想到的几个方法可以做到这一点,如增量文件夹名称或随机编号的文件夹名称,但我想知道,其他人是如何解决这个问题呢?
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. 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?
推荐答案更新:新增File.Exists每个注释检查(2012君19)
Update: Added File.Exists check per comment (2012-Jun-19)
下面是我在VB.NET中使用过。基本相同,presented,但我通常不想立即创建该文件夹。
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.
的优点是使用GetRandomFilename是,它不创建一个文件,所以您不必如果您使用的名称不是文件的东西等进行清理。就像使用它的文件夹名称。
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
随机文件名示例:
C:\的Documents and Settings \用户名\本地设置的\ Temp \ u3z5e0co.tvq
C:\Documents and Settings\username\Local Settings\Temp\u3z5e0co.tvq
下面是一个使用GUID来获得临时文件夹名称的变化。
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 示例:
C:\的Documents and Settings \用户名\本地设置的\ Temp \ 2dbc6db7-2d45-4b75-b27f-0bd492c60496
C:\Documents and Settings\username\Local Settings\Temp\2dbc6db7-2d45-4b75-b27f-0bd492c60496
更多推荐
创建临时文件夹
发布评论