System.Webの参照設定が必要
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Static l_blnFlg As Boolean = False
If l_blnFlg Then
MsgBox("実行中!")
Return
End If
l_blnFlg = True
Dim l_intCount As Integer
Try
Dim l_strRoot As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "検索結果")
Dim l_dirSch As New IO.DirectoryInfo(TextBox1.Text)
Call CopyFile(l_strRoot, l_dirSch.FullName, "*.txt", l_dirSch, l_intCount)
Me.Label1.Text = ""
Call MsgBox(String.Format("{0}件の処理が終了!", l_intCount))
Process.Start(l_strRoot)
Catch ex As Exception
Me.Label1.Text = ex.Message
Call MsgBox("エラー発生")
Finally
l_blnFlg = False
End Try
End Sub
'ファイルの検索コピー
Private Sub CopyFile(ByVal p_strCopyRoot As String, ByVal p_strSchRoot As String, ByVal p_strSchFile As String, ByVal p_dSch As IO.DirectoryInfo, ByRef p_intCount As Integer)
Application.DoEvents()
Me.Label1.Text = String.Format("[{0}]を検索中...", p_dSch.FullName)
For Each l_fInf As IO.FileInfo In p_dSch.GetFiles(p_strSchFile)
p_intCount += 1
'フォルダを作成
Dim l_strDir As String = GetFolderPath(p_strCopyRoot, p_strSchRoot, l_fInf.DirectoryName)
Call IO.Directory.CreateDirectory(l_strDir)
'ファイルのコピー(上書き=TRUE)
Dim l_strFile As String = IO.Path.Combine(l_strDir, l_fInf.Name)
Dim l_finfCopy As IO.FileInfo = l_fInf.CopyTo(l_strFile, True)
'二回目の実行用のために、読取専用を外す
l_finfCopy.IsReadOnly = False
Next
'ファイルの検索コピーを再帰
For Each l_dSubInf As IO.DirectoryInfo In p_dSch.GetDirectories
Call CopyFile(p_strCopyRoot, p_strSchRoot, p_strSchFile, l_dSubInf, p_intCount)
Next
End Sub
'フォルダパスの取得
Function GetFolderPath(ByVal p_strCopyRoot As String, ByVal p_strSchRoot As String, ByVal p_strSchPath As String) As String
Dim l_uri As New Uri(p_strSchRoot)
Dim l_strPath相対 As String = System.Web.HttpUtility.UrlDecode(l_uri.MakeRelative(New Uri(p_strSchPath)))
Return IO.Path.Combine(p_strCopyRoot, l_strPath相対)
End Function
補足
ご回答頂きありがとうございました。 早速試してみたところ、下記の行からエラーが出ていますが、よろしくご指導お願いいたします。 エラーは同じで”VB 名前空間のステートメントが無効です”となります。 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Private Sub CopyFile(ByVal p_strCopyRoot As String, ByVal p_strSchRoot As String, ByVal p_strSchFile As String, ByVal p_dSch As IO.DirectoryInfo, ByRef p_intCount As Integer) Function GetFolderPath(ByVal p_strCopyRoot As String, ByVal p_strSchRoot As String, ByVal p_strSchPath As String) As String