【问题标题】:Create folder if path does not exist?如果路径不存在,创建文件夹?
【发布时间】:2013-07-16 20:33:18
【问题描述】:

我正在使用下面的代码将 pdf 文件保存到网络位置。如果路径不存在,是否有可能创建文件夹?下面的代码只是在我需要创建用户名文件夹时将用户名添加到文件名中?

    Dim Doc1 As New Document
    Dim path As String = "\\Servername\PDFs\" + Session("Username")


    Dim myUniqueFileName = String.Format("{0}.pdf", random)
    Dim combinedData As String = path & myUniqueFileName
    PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))
    Doc1.Open()
    Dim test As String
    test = Session("PDF")
    Doc1.Add(New Paragraph(test))

    Doc1.Close()

【问题讨论】:

    标签: asp.net vb.net pdf-generation


    【解决方案1】:

    当然,像这样:

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

    【讨论】:

    • 看起来创建了文件夹但 pdf 没有保存到新目录?它保存到根目录?
    • 您是否在Session("Username")? 中包含“\”我将假设您没有。如果是这种情况,您可能需要更改此行:Dim myUniqueFileName = String.Format("{0}.pdf", random)Dim myUniqueFileName = String.Format("\{0}.pdf", random)
    • 另外,System.IO.Path.Combine 会为你处理斜线(这样你就不会得到 0 或 2 个斜线等)。示例:Path.Combine(path, myUniqueFileName)
    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 2022-11-27
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2010-11-22
    • 2011-01-19
    相关资源
    最近更新 更多