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

后端 未结 12 1284
孤独总比滥情好
孤独总比滥情好 2020-12-23 11:29

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, wi

相关标签:
12条回答
  • 2020-12-23 11:54

    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.

    0 讨论(0)
  • 2020-12-23 11:58

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

    Dim objFSO, strFolder
    strFolder = "C:\Temp"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Not objFSO.FolderExists(strFolder) Then
       objFSO.CreateFolder(strFolder)
    End If
    
    0 讨论(0)
  • 2020-12-23 12:00

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

    (You may need: Imports System.IO)

    0 讨论(0)
  • 2020-12-23 12:06

    Just do this:

            Dim sPath As String = "Folder path here"
        If (My.Computer.FileSystem.DirectoryExists(sPath) = False) Then
            My.Computer.FileSystem.CreateDirectory(sPath + "/<Folder name>")
        Else
            'Something else happens, because the folder exists
        End If
    

    I declared the folder path as a String (sPath) so that way if you do use it multiple times it can be changed easily but also it can be changed through the program itself.

    Hope it helps!

    -nfell2009

    0 讨论(0)
  • 2020-12-23 12:08

    Directory.CreateDirectory() should do it. 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:

    Hope that helps.

    0 讨论(0)
  • 2020-12-23 12:08
    If Not Directory.Exists(somePath) then
        Directory.CreateDirectory(somePath)
    End If
    
    0 讨论(0)
提交回复
热议问题