tags:

views:

10742

answers:

12

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.

I want my program to download the apps to 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.

+2  A: 

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

(You may need: Imports System.IO)

Geoffrey Chetwood
A: 

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

Chris Bilson
+7  A: 
If(Not System.IO.Directory.Exists(YourPath)) Then
    System.IO.Directory.CreateDirectory(YourPath)
Quintin Robinson
Don't bother checking to see if it exists, it just wastes time.CreateDirectory won't throw an exception. Also, someone could create the directory between the time you checked and the time you created it, making the check even more pointless.
Jonathan Allen
+2  A: 

Try the System.IO.DirectoryInfo class.

The sample from 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
+4  A: 

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

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

It will ensure that the directory is there.

MagicKat
So will Directory.CreateDirectory(path). The pre-check isn't needed.
Jonathan Allen
A: 

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.

Dave
+2  A: 

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.

Mostlyharmless
+1  A: 

(imports System.IO)

if Not Directory.Exists(Path) then
  Directory.CreateDirectory(Path)
end if
Wayne
You don't need to check if it exists first, CreateDirectory will do that for you.
Jonathan Allen
+1  A: 

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
Rick
A: 

Hi there,

I see how this would work, what would be the process to create a dialog box that allows the user name the folder and place it where you want to.

Cheers

A: 

if Not Directory.Exists(somePath) then

Directory.CreateDirectory(somePath)

end if

A: 

The Program Files folder has special permission that stop me from using a code like this to create a directory in within Program Files.

files