vb.net 如果文件夹不存在,如何在 VB 中创建文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/85996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:49:19  来源:igfitidea点击:

How do I create a folder in VB if it doesn't exist?

vb.netinstalldirectory

提问by Quintin Robinson

I wrote myself a little downloading application so that I could easily grab a set of files from my server and put them all onto a new pc with a clean install of Windows, without actually going on the net. Unfortunately I'm having problems creating the folder I want to put them in and am unsure how to go about it.

我给自己写了一个下载应用程序,这样我就可以轻松地从我的服务器上抓取一组文件,并将它们全部放到一台全新安装了 Windows 的新电脑上,而无需实际上网。不幸的是,我在创建文件夹时遇到了问题,我想把它们放进去,我不确定如何去做。

I want my program to download the apps to program files\any name here\

我希望我的程序将应用程序下载到 program files\any name here\

So basically I need a function that checks if a folder exists, and if it doesn't it creates it.

所以基本上我需要一个函数来检查文件夹是否存在,如果不存在则创建它。

回答by Quintin Robinson

If(Not System.IO.Directory.Exists(YourPath)) Then
    System.IO.Directory.CreateDirectory(YourPath)
End If

回答by MagicKat

Under System.IO, there is a class called Directory. Do the following:

在 System.IO 下,有一个名为 Directory 的类。请执行下列操作:

If Not Directory.Exists(path) Then
    Directory.CreateDirectory(path)
End If

It will ensure that the directory is there.

它将确保目录在那里。

回答by Rick

Since the question didn't specify .NET, this should work in VBScript or VB6.

由于问题没有指定 .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

回答by Guy Starbuck

Try the System.IO.DirectoryInfoclass.

试试System.IO.DirectoryInfo类。

The sample from MSDN:

来自 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

回答by GEOCHET

Try this: Directory.Exists(TheFolderName)and Directory.CreateDirectory(TheFolderName)

试试这个:Directory.Exists(TheFolderName)Directory.CreateDirectory(TheFolderName)

(You may need: Imports System.IO)

(您可能需要:Imports System.IO

回答by Chris Bilson

VB.NET? System.IO.Directory.Exists(string path)

VB.NET?System.IO.Directory.Exists(字符串路径)

回答by Mostlyharmless

Directory.CreateDirectory() should do it. http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx

Directory.CreateDirectory() 应该这样做。 http://msdn.microsoft.com/en-us/library/system.io.directory.createdirectory(VS.71).aspx

Also, in Vista, you probably cannot write into C: directly unless you run it as an admin, so you might just want to bypass that and create the dir you want in a sub-dir of C: (which i'd say is a good practice to be followed anyways. -- its unbelievable how many people just dump crap onto C:

此外,在 Vista 中,除非您以管理员身份运行,否则您可能无法直接写入 C:,因此您可能只想绕过它并在 C: 的子目录中创建您想要的目录(我想说的是无论如何,这是一个很好的做法。-令人难以置信的是,有多少人只是将废话倾倒到 C 上:

Hope that helps.

希望有帮助。

回答by Wayne

(imports System.IO)

(导入 System.IO)

if Not Directory.Exists(Path) then
  Directory.CreateDirectory(Path)
end if

回答by Wayne

If Not Directory.Exists(somePath) then
    Directory.CreateDirectory(somePath)
End If

回答by Dave

You should try using the File System Object or FSO. There are many methods belonging to this object that check if folders exist as well as creating new folders.

您应该尝试使用文件系统对象或 FSO。有许多属于此对象的方法可以检查文件夹是否存在以及创建新文件夹。