连接两个路径字符串以获得最终路径?

Connecting two path strings to get the final path?

我正在尝试将 excel 文件保存到特定路径。 所以基本上,当我单击按钮时,我正在创建一个文件夹,并希望将文件保存在该文件夹中。 创建的文件夹以当前月份作为名称。我正在尝试保存到当前月份的文件夹中。

    'Create folder as Month Name. Save filename as date inside "month".
    Dim sDate As String = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")
    Dim sMonth As String = DateTime.Now.ToString("MMMM")
    Dim sFolder = Application.StartupPath & "\Resources\Excel\"


    My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))

    Dim sfinal = Path.Combine(sFolder, sMonth)
    xlSh.SaveAs(sfinal & Format(sDate) & ".xlsx")

    xlApp.Workbooks.Close()
    xlApp.Quit()

事实上,这段代码没有给我任何错误。但是,它没有创建名为 "March" <-current month 的文件夹并保存在其中,而是将文件保存在 \Excel\ 中,并且还在同一位置创建了文件夹。

您似乎并没有真正将保存路径设置为创建的目录。相反,我相信您将月份附加到 xlSh.SaveAs(sFinal & Format(sDate) & ".xlsx") 文件名的开头。基本上(虽然我不确定具体的命令)你需要在创建后导航到你创建的文件夹。 可能格式为

My.Computer.FileSystem.ChangeDirectory(sFolder & Format(sMonth))

虽然我不知道我写的那个特定命令实际上存在。

经过长时间的极度痛苦,我终于做到了! 显然我错过了一个“\” 由于 "sMonth" 成为动态名称,后来我想将其用作路径,并将文件保存在该文件夹中。我需要简单地在 sMonth 之后加上那个“\”,告诉它保存在里面。

在我意识到这一点之前...我已经分解并尽可能地简化了代码,这样我就可以在逻辑上连接各个部分。我最终得到的是略有不同的东西。现在 SaveAS 正确地将文件保存在新文件夹中。

    Dim sDate As String
    sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")

    Dim sMonth As String
    sMonth = DateTime.Now.ToString("MMMM")

    Dim sFileName As String
    sFileName = sDate + ".xlsx"

    Dim sFolder As String
    sFolder = Application.StartupPath & "\Resources\Excel\"


    Dim sfinal As String
    sfinal = (sFolder & sMonth & "\") '<- this thingie here o.O

    My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))

    xlSh.SaveAs(sfinal & Format(sFileName))

    xlApp.Workbooks.Close()
    xlApp.Quit()

感谢您的帮助。

对于那些一直想知道我在做什么的人,这是完整的子。如果有人需要类似的东西。感谢您的支持。问题已解决。

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click

    Dim xlApp As Excel.Application
    Dim xlSh As Excel.Worksheet


    xlApp = New Excel.Application


    xlApp.Workbooks.Add()
    xlSh = xlApp.Workbooks(1).Worksheets(1)

    'Items from listbox1 to be exported into excel, second row, second column.
    Dim row As Integer = 2
    Dim col As Integer = 2
    For i As Integer = 0 To ListBox1.Items.Count - 1
        xlSh.Cells(row, col) = ListBox1.Items(i)
        row = row + 1
    Next
    row += 1
    col = 1

    'Items from listbox2 to be exported into excel, second row, third column.
    Dim row2 As Integer = 2
    Dim col2 As Integer = 3
    For i As Integer = 0 To ListBox2.Items.Count - 1
        xlSh.Cells(row2, col2) = ListBox2.Items(i)
        row2 = row2 + 1
    Next
    row2 += 1
    col2 = 1


    'Create folder as Month Name. Save filename as date inside that folder.

    'Make filename be yyyy-MM-DD_HH-mm-ss
    Dim sDate As String
    sDate = DateTime.Now.ToString("yyyy-MM-dd") & "_" & DateTime.Now.ToString("HH-mm-ss")

    'This will be used as name for the new folder.
    Dim sMonth As String
    sMonth = DateTime.Now.ToString("MMMM")

    'Filename + extension.
    Dim sFileName As String
    sFileName = sDate + ".xlsx"

    'This is the path.
    Dim sFolder As String
    sFolder = Application.StartupPath & "\Resources\Excel\"

    'This is the path combined with sMonth to make the final path.
    Dim sfinal As String
    sfinal = (sFolder & sMonth & "\")

    'Check if folder with the name sMonth already exists.
    If Dir(sFolder, vbDirectory) = sMonth Then

        'If it exist, then simply save the file inside the folder.
        xlSh.SaveAs(sfinal & Format(sFileName))
    Else
        'If it doesn't exist:
        'This is the creation of sMonth folder, inside "\excel\.
        My.Computer.FileSystem.CreateDirectory(sFolder & Format(sMonth))
        'This saves the excel file at path sfinal, with filename of sFileName
        xlSh.SaveAs(sfinal & Format(sFileName))

    End If



    'Close everything.
    xlApp.Workbooks.Close()
    xlApp.Quit()
End Sub

您可以使用以下函数(类似于 .NET System.IO.Path.Combine)

Function PathCombine(path1 As String, path2 As String)

    Dim combined As String

    combined = path1
    If Right$(path1, 1) <> Application.PathSeparator Then
        combined = combined & Application.PathSeparator
    End If
    combined = combined & path2
    PathCombine = combined

End Function

希望对您有所帮助!