创建临时文件夹 [英] Creating temporary folders

查看:232
本文介绍了创建临时文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,需要为应用程序创建多个临时文件夹的程序。这些将不会被用户看到。该应用程序是用VB.net。我能想到的几个方法可以做到这一点,如增量文件夹名称或随机编​​号的文件夹名称,但我想知道,其他人是如何解决这个问题呢?

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?

推荐答案

更新:新增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.

的优点是使用<一个href="http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx">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

这篇关于创建临时文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆