If Directory Exists Dont Create Again Vb
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.
Mark Hurd
10.4k 10 gold badges 65 silver badges 99 bronze badges
asked Sep 17, 2008 at 18:10
1
12 Answers 12
If Not System.IO.Directory.Exists(YourPath) Then System.IO.Directory.CreateDirectory(YourPath) End If
schlebe
2,788 4 gold badges 36 silver badges 45 bronze badges
answered Sep 17, 2008 at 18:12
Quintin RobinsonQuintin Robinson
78.9k 14 gold badges 121 silver badges 132 bronze badges
4
-
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.
Oct 4, 2008 at 5:45
-
Yes, but if the folder exists then he may not want to copy the app over, so the check will be useful in that situation.
Feb 23, 2012 at 6:54
-
If that case, he should check if the specific file exists rather than just the directory.
Sep 13, 2013 at 14:53
-
I got the same problem in Silverlight, but this answer does not help...hmm. It seems like there is a security rule in Silverlight that does not allow to access to path
May 14, 2018 at 7:25
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.
answered Sep 17, 2008 at 18:13
MagicKatMagicKat
9,429 6 gold badges 29 silver badges 43 bronze badges
2
-
So will Directory.CreateDirectory(path). The pre-check isn't needed.
Oct 4, 2008 at 5:46
-
That looks (almost) identical to the marked answer o.O
Mar 15, 2014 at 1:37
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 answered Sep 17, 2008 at 18:12
Guy StarbuckGuy Starbuck
21.1k 6 gold badges 52 silver badges 63 bronze badges
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 1
-
A nice example for the folks working in traditional VB/VBA but I have a nit-picky syntax issue: the parentheses don't actually do anything in the statement "objFSO.CreateFolder (strFolder)". They just force the variable's string-value to a (string) value before passing it to CreateFolder. The correct syntax should be either "objFSO.CreateFolder strFolder" or "Call objFSO.CreateFolder(strFolder)".
Sep 26, 2019 at 19:11
Try this: Directory.Exists(TheFolderName) and Directory.CreateDirectory(TheFolderName)
(You may need: Imports System.IO)
answered Sep 17, 2008 at 18:11
GEOCHETGEOCHET
20.8k 15 gold badges 72 silver badges 98 bronze badges
0
VB.NET? System.IO.Directory.Exists(string path)
answered Sep 17, 2008 at 18:12
Chris BilsonChris Bilson
1,844 1 gold badge 16 silver badges 20 bronze badges
0
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.
answered Sep 17, 2008 at 18:15
(imports System.IO)
if Not Directory.Exists(Path) then Directory.CreateDirectory(Path) end if
answered Sep 17, 2008 at 18:16
WayneWayne
36.5k 4 gold badges 38 silver badges 49 bronze badges
1
-
You don't need to check if it exists first, CreateDirectory will do that for you.
Oct 4, 2008 at 5:47
If Not Directory.Exists(somePath) then Directory.CreateDirectory(somePath) End If
answered Aug 7, 2009 at 17:02
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.
answered Sep 17, 2008 at 18:13
DaveDave
89 1 gold badge 2 silver badges 11 bronze badges
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
answered Mar 24, 2009 at 2:12
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
answered Oct 16, 2013 at 17:30
BaeFellBaeFell
645 1 gold badge 8 silver badges 26 bronze badges
Source: https://stackoverflow.com/questions/85996/how-do-i-create-a-folder-in-vb-if-it-doesnt-exist
Please note that you may run in to permissions issues writing to \Program Files\, particularly under Vista. You should consider a different location.
Sep 17, 2008 at 18:17