2010-02-15 79 views
4

我正在研究一個VB.NET程序,它會自動將我的工作備份到我的FTP服務器。到目前爲止,我能夠上傳單個文件,通過指定使用這個文件名:VB.NET遍歷文件和目錄

 'relevant part - above is where FTP object is instantiated 
     Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead() 

      Try 
       'Stream to which the file to be upload is written 
       Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream() 

       'Read from the file stream 2kb at a time 
       Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength) 

       'Till Stream content ends 
       Do While contentLen <> 0 
        ' Write Content from the file stream to the FTP Upload Stream 
        _Stream.Write(buff, 0, contentLen) 
        contentLen = _FileStream.Read(buff, 0, buffLength) 
       Loop 
       _Stream.Close() 
       _Stream.Dispose() 
       _FileStream.Close() 
       _FileStream.Dispose() 

       ' Close the file stream and the Request Stream 


      Catch ex As Exception 
       MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 
      End Try 

現在我希望能夠通過目錄遍歷(包含子目錄)和遞歸調用上面的函數。我在保存目錄中的文件時遇到問題。

我的問題是,我如何循環併發送每個文件到上面的上傳功能?我可以只發送文件名到一個數組,或者是有一些類型的系統對象來處理這個(如System.IO.Directory)

僞的什麼,我試圖做

   For Each Sub_directory In Source_directory 

        For Each File in Directory 
         'call the above code to transfer the file 
        Next 

       'start next subdirectory 
       Next 

我試圖複製與子目錄完整的整個目錄結構。我第一次嘗試將所有文​​件轉儲到一個目錄中。

回答

8

你可以經歷這樣每個目錄和文件中使用遞歸:

Public Shared Sub ForEachFileAndFolder(ByVal sourceFolder As String, _ 
             ByVal directoryCallBack As Action(Of DirectoryInfo), _ 
             ByVal fileCallBack As Action(Of FileInfo)) 

    If Directory.Exists(sourceFolder) Then 
     Try 
      For Each foldername As String In Directory.GetDirectories(sourceFolder) 
       If directoryCallBack IsNot Nothing Then 
        directoryCallBack.Invoke(New DirectoryInfo(foldername)) 
       End If 

       ForEachFileAndFolder(foldername, directoryCallBack, fileCallBack) 
      Next 
     Catch ex As UnauthorizedAccessException 
      Trace.TraceWarning(ex.Message) 
     End Try 

     If fileCallBack IsNot Nothing 
      For Each filename As String In Directory.GetFiles(sourceFolder) 
       fileCallBack.Invoke(New FileInfo(filename)) 
      Next 
     End If 
    End If 
End Sub 

編輯:代表的用法:

ForEachFileAndFolder("yourPath", AddressOf dirAction, Addressof fileAction) 

Public Sub dirAction(Byval dirInfo As DirectoryInfo) 
    ' do something here ' 
End Sub 

相同的FileAction。

+0

謝謝鮑比,這似乎是我正在尋找。不過,我從來沒有用「As Action」作爲論據。在ForEachFileAndFolder的初始調用中,我將作爲directoryCallBack和fielCallBack傳遞什麼?這些參數應該是我想要在每個對象上運行的函數嗎?例如FTP傳輸函數= fileCallBack? – tpow 2010-02-15 18:35:18

+0

@cinqoTimo:添加了一個關於如何起訴代表的例子。還在Nothing中添加了一個檢查,也許你不想對文件夾做些什麼,然後你可以傳入Nothing。 – Bobby 2010-02-15 21:43:55

+0

你是男人,它的工作原理......所以一個代表基本上是一個私人功能。我有一個大概的想法,我沒有OOP背景 - 主要是HTML/CSS/PHP。謝謝你的時間。 – tpow 2010-02-15 23:36:36

1

你必須分開處理目錄和文件,但它仍然非常簡單。

System.IO.DirectoryInfo強制指定目錄內的列表文件和子目錄。尋找屬性目錄和文件。

你想要做的是指定一個函數,它將首先讀取所有文件,然後讀取DirectoryInfo中的目錄。對於這些文件,你可以做你想做的事情(上傳我認爲),一旦你打開目錄,使用子目錄作爲參數遞歸地調用你的函數。這將去所有的子目錄和文件。

僞代碼(我稱之爲僞代碼COS'我不知道確切的語法)

 

Sub UploadDirectory(oDirInfo as DirectoryInfo) 

    For eaco oFile as FileInfo in oDirINfo.Files 
    'Do your thing 
    Next 

    For each oDir as DirectoryInfo as oDirInfo.Directories 
    ' Do some stuff if you need to separate directories 
    UploadDirectory(odir) 
End sub 

希望有所幫助。