2013-08-21 186 views
0

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

如果我给它一个这样的路径: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

我假设你刚刚开始使用这段代码:“如果astop.Text = 1 = False Then”就是假的。 – rheitzman

+0

是的,但astop在单击取消时被用于第二种形式,并且在循环内检查... –

+1

astop.Text = 1 = False实际上是astop.Text =(1 = False)。 (1 = False)总是False。 (1 = False)可能被VB转换为“False”,但它永远不会是“True” – rheitzman

回答

2

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

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

嗯...我想我可以先创建它们,但是,我正在寻找最有效的方式来做这件事...程序可能与大量文件一起工作,所以即使几毫秒的性能差异count ... –

+0

无论如何,您的程序必须创建这些文件夹,无论您使用什么方法。在任何情况下,您的代码都必须调用系统API的文件夹创建功能。所以你用任何其他方法都无法赢得比赛。 – ataman