简体   繁体   English

检查目录的第一部分是否已经存在

[英]Checking if the first part of a directory already exists

I have created an application that creates a folder and a few more documents for a new site the User is surveying. 我创建了一个应用程序,该应用程序为用户正在调查的新站点创建了一个文件夹和一些其他文档。

Once the User has entered the necessary Site details, a check is done to see if the folder exists, and if it doesn't, it creates it. 用户输入必要的站点详细信息后,将检查文件夹是否存在,如果不存在,则会创建该文件夹。

Here is a sample of the code I am using to achieve this: 这是我用来实现此目的的代码示例:

Public Class Form1

    Dim SiteName As String
    Dim SiteNumber As String

    Private Sub btnCreateFolder_Click(sender As Object, e As EventArgs) _
        Handles btnCreateFolder.Click

        SiteName = txtSiteName.Text
        SiteNumber = txtSiteNumber.Text

        CurrentSiteLoc = "C:\VBA\" & SiteNumber & " " & SiteName

        If Not IO.Directory.Exists(CurrentSiteLoc) Then
            MkDir(CurrentSiteLoc)

        Else
            MessageBox.Show("Folder already exists.")

        End If

    End Sub

End Class

This check works perfectly if the User always uses the correct SiteName, however each site is defined by its Site Number. 如果用户始终使用正确的SiteName,则此检查效果很好,但是每个站点均由其站点号定义。

"524128 Corner's Stone" “ 524128角落的石头”

This is an example of a possible site folder name, but the User might also decide to add some more info so it could be created with the name: 这是一个可能的站点文件夹名称的示例,但是用户可能还会决定添加更多信息,以便可以使用以下名称创建它:

"524128 Corner's Stone (L6)" “ 524128角石(L6)”

What is the best way to search my directory for a folder that has the same Site Number, instead of the same folder name? 在目录中搜索具有相同站点编号而不是相同文件夹名称的文件夹的最佳方法是什么?

You can use the search pattern argument of the Directory.GetDirectories() -method: 您可以使用Directory.GetDirectories()方法的搜索模式参数:

If System.IO.Directory.GetDirectories("C:\VBA\", SiteNumber & " *").Count = 0 Then
    MkDir(CurrentSiteLoc)
Else
    MessageBox.Show("Folder already exists.")
End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM