如果VB中的文件夹不存在,如何创建它?

发布于 2024-07-04 09:24:40 字数 230 浏览 7 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

溺深海 2024-07-11 09:24:40

只需这样做:

        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

我将文件夹路径声明为字符串(sPath),这样,如果您多次使用它,则可以轻松更改它,但也可以通过程序本身进行更改。

希望能帮助到你!

-nfell2009

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

空城仅有旧梦在 2024-07-11 09:24:40

(导入System.IO)

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

(imports System.IO)

if Not Directory.Exists(Path) then
  Directory.CreateDirectory(Path)
end if
栖迟 2024-07-11 09:24:40
If Not Directory.Exists(somePath) then
    Directory.CreateDirectory(somePath)
End If
If Not Directory.Exists(somePath) then
    Directory.CreateDirectory(somePath)
End If
甜妞爱困 2024-07-11 09:24:40

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

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.

魂归处 2024-07-11 09:24:40

我了解这是如何工作的,创建一个允许用户命名文件夹并将其放置在您想要的位置的对话框的过程是什么。

干杯

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

胡渣熟男 2024-07-11 09:24:40

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

(您可能需要:Imports System.IO

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

(You may need: Imports System.IO)

澜川若宁 2024-07-11 09:24:40

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

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

箹锭⒈辈孓 2024-07-11 09:24:40

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

另外,在 Vista 中,您可能无法直接写入 C: ,除非您以管理员身份运行它,所以您可能只是想绕过它并在 C: 的子目录中创建你想要的目录(我想说这是一个应该遵循的好习惯。--令人难以置信的是有多少人只是将垃圾倾倒到 C:

希望有帮助。

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.

南风几经秋 2024-07-11 09:24:40
If Not System.IO.Directory.Exists(YourPath) Then
    System.IO.Directory.CreateDirectory(YourPath)
End If
If Not System.IO.Directory.Exists(YourPath) Then
    System.IO.Directory.CreateDirectory(YourPath)
End If
季末如歌 2024-07-11 09:24:40

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

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

它将确保该目录存在。

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.

花开半夏魅人心 2024-07-11 09:24:40

尝试使用 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

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
别挽留 2024-07-11 09:24:40

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文