如何在创建文件时创建子文件夹?

时间:2013-08-21 14:11:44

标签: vb.net file stream copy

我正在创建一个应用程序,它涉及读取现有文件的块并将它们写入一个新文件...现在,问题是我当前使用的代码不会创建子文件夹然后提交文件,如果完整路径是给定...

如果我给它一个这样的路径:C:\ folder1 \ folder2 \ file.mp3它给出一个错误,因为文件夹folder1和2不存在,但是,我希望它创建这些子文件夹,如果他们不在创建文件时存在...谢谢......这是我的代码:

  Dim bytesRead As Integer
Dim buffer(40096) As Byte

Using inFile As New System.IO.FileStream(arch, IO.FileMode.Open, IO.FileAccess.Read)
    Using outFile As New System.IO.FileStream(path & "\" & f, IO.FileMode.Create, IO.FileAccess.Write)
        inFile.Seek(StartAt, IO.SeekOrigin.Begin)

        Do
            If astop.Text = 1 = False Then
                If CurrentFsize - currtotal < buffer.Length Then
                    ReDim buffer(CurrentFsize - currtotal)
                End If

                bytesRead = inFile.Read(buffer, 0, buffer.Length)
                If bytesRead > 0 Then
                    outFile.Write(buffer, 0, bytesRead)
                    currtotal += bytesRead



                End If
            Else
                Exit Do
                Exit Do

            End If
            Application.DoEvents()

        Loop While bytesRead > 0 AndAlso currtotal < CurrentFsize
    End Using
End Using

1 个答案:

答案 0 :(得分:2)

Yout应该在创建输出文件之前创建path的目录和子目录:

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