vb.net-如果不存在,如何在VB中创建文件夹?
我为自己编写了一个小小的下载应用程序,以便我可以轻松地从服务器上获取一组文件,然后将它们全部放入带有全新安装的Windows的新PC上,而无需实际运行网络。 不幸的是,我在创建要放入的文件夹时遇到了问题,不确定如何处理。
我希望我的程序将应用程序下载到program files\any name here\
因此,基本上我需要一个函数来检查文件夹是否存在,如果不存在,它将创建该文件夹。
12个解决方案
149 votes
If(Not System.IO.Directory.Exists(YourPath)) Then
System.IO.Directory.CreateDirectory(YourPath)
End If
Quintin Robinson answered 2020-01-27T14:28:41Z
22 votes
在System.IO下,有一个名为Directory的类。请执行下列操作:
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
这将确保该目录在那里。
MagicKat answered 2020-01-27T14:29:05Z
11 votes
由于问题未指定.NET,因此它应在VBScript或VB6中工作。
Dim objFSO, strFolder
strFolder = "C:\Temp"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
objFSO.CreateFolder(strFolder)
End If
Rick answered 2020-01-27T14:29:25Z
10 votes
试试System.IO.DirectoryInfo类。
来自MSDN的示例:
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\MyDir")
Try
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("That path exists already.")
Return
End If
' Try to create the directory.
di.Create()
Console.WriteLine("The directory was created successfully.")
' Delete the directory.
di.Delete()
Console.WriteLine("The directory was deleted successfully.")
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Guy Starbuck answered 2020-01-27T14:29:49Z
5 votes
VB.NET? System.IO.Directory.Exists(字符串路径)
Chris Bilson answered 2020-01-27T14:30:09Z
5 votes
试试这个:Imports System.IO和Directory.CreateDirectory(TheFolderName)
(您可能需要:Imports System.IO)
GEOCHET answered 2020-01-27T14:30:33Z
4 votes
Directory.CreateDirectory()应该这样做。[http://msdn.microsoft.com/zh-cn/library/system.io.directory.createdirectory(VS.71).aspx]
另外,在Vista中,除非您以管理员身份运行它,否则您可能无法直接写入C :,所以您可能只想绕过它,并在C:的子目录中创建所需的目录(我想说的是 无论如何都要遵循的一个好习惯-令人难以置信的是有多少人将废话扔到C上:
希望能有所帮助。
Mostlyharmless answered 2020-01-27T14:31:04Z
4 votes
(导入System.IO)
if Not Directory.Exists(Path) then
Directory.CreateDirectory(Path)
end if
Wayne answered 2020-01-27T14:31:23Z
3 votes
If Not Directory.Exists(somePath) then
Directory.CreateDirectory(somePath)
End If
Siddharth Rout answered 2020-01-27T14:31:39Z
1 votes
您应该尝试使用文件系统对象或FSO。 属于该对象的方法有很多,它们可以检查文件夹是否存在以及创建新文件夹。
Dave answered 2020-01-27T14:31:59Z
0 votes
我知道它是如何工作的,创建对话框的过程将是什么,该对话框允许用户命名文件夹并将其放置在所需的位置。
干杯
answered 2020-01-27T14:32:24Z
0 votes
只是这样做:
Dim sPath As String = "Folder path here"
If (My.Computer.FileSystem.DirectoryExists(sPath) = False) Then
My.Computer.FileSystem.CreateDirectory(sPath + "/")
Else
'Something else happens, because the folder exists
End If
我将文件夹路径声明为String(sPath),这样,如果您多次使用它,则可以轻松更改它,也可以通过程序本身对其进行更改。
希望能帮助到你!
-nfell2009
BaeFell answered 2020-01-27T14:32:57Z