创建临时文件夹

编程入门 行业动态 更新时间:2024-10-12 03:27:23
本文介绍了创建临时文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的工作,需要为应用程序创建多个临时文件夹的程序。这些将不会被用户看到。该应用程序是用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

更多推荐

创建临时文件夹

本文发布于:2023-07-06 07:42:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:临时文件夹

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!