簡體   English   中英

檢查目錄的第一部分是否已經存在

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

我創建了一個應用程序,該應用程序為用戶正在調查的新站點創建了一個文件夾和一些其他文檔。

用戶輸入必要的站點詳細信息后,將檢查文件夾是否存在,如果不存在,則會創建該文件夾。

這是我用來實現此目的的代碼示例:

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

如果用戶始終使用正確的SiteName,則此檢查效果很好,但是每個站點均由其站點號定義。

“ 524128角落的石頭”

這是一個可能的站點文件夾名稱的示例,但是用戶可能還會決定添加更多信息,以便可以使用以下名稱創建它:

“ 524128角石(L6)”

在目錄中搜索具有相同站點編號而不是相同文件夾名稱的文件夾的最佳方法是什么?

您可以使用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