vb.net 如果路径不存在,创建文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17686423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:17:10  来源:igfitidea点击:

Create folder if path does not exist?

asp.netvb.netpdf-generation

提问by user1342164

I am using the code below to save a pdf file to a network location. Is it possible that if the path does not exist that it creates the folder? The code below is just adding the username to the file name when I need it to create the username's folder?

我正在使用下面的代码将 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()

回答by Dave H

Absolutely, like this:

绝对的,像这样:

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