我想使用VB.net将HTML文件从一个位置复制到另一个位置 当我使用三个FileCopy,System.IO.File.Copy,My.Computer.FileSystem.CopyFile中的任何一个时 它只复制文件,而不复制" filename_files"包含其关联图像和脚本的文件夹。
我想以编程方式执行的操作是将a.html复制到另一个位置,如b.html
当我这样做并打开b.html时,它打开它没有任何图像和脚本。
请帮忙
答案 0 :(得分:1)
您可以使用以下两种方法共同复制包含脚本和图像的文件夹,使用内置方法FileCopy
复制HTML文件,并使用以下方法复制所需的文件夹。
我找到了第一个在here
的给定路径中返回文件数组的方法Public Function FileList(Mask As String) As String()
Dim sWkg As String
Dim sAns() As String
Dim lCtr As Long
ReDim sAns(0) As String
sWkg = Dir(Mask, vbNormal)
Do While Len(sWkg)
If sAns(0) = "" Then
sAns(0) = sWkg
Else
lCtr = UBound(sAns) + 1
ReDim Preserve sAns(lCtr) As String
sAns(lCtr) = sWkg
End If
sWkg = Dir
Loop
FileList = sAns
End Function
现在使用上述方法和以下方法,您可以通过指定源路径和目标路径来复制文件夹。该方法将返回boolean值,指定是否复制了文件夹。
Public Function FolderCopy(ByVal SourceFolder As String, ByVal TargetFolder As String) As Boolean
Dim flist() As String
Dim sURL As String = New String(SourceFolder)
Dim tURL As String = New String(TargetFolder)
Dim i As Integer
Dim slashpos As Long
If Not Directory.Exists(tURL) Then
slashpos = InStrRev(sURL, "\") 'Get position of last occurrence if '\' in given path
If slashpos <> sURL.Length Then 'Check if URL does not have slash at its end
sURL = sURL & "\" 'Add slash at URL end
End If
flist = FileList(sURL)
slashpos = InStrRev(tURL, "\") 'Get position of last occurrence if '\' in given path
If slashpos = tURL.Length Then
tURL = tURL.Substring(0, tURL.Length - 1)
End If
slashpos = InStrRev(tURL, "\")
Try
Directory.CreateDirectory(tURL)
For i = 0 To flist.Length - 1
FileCopy(sURL & flist(i), tURL & "\" & flist(i))
Next
FolderCopy = True
Catch ex As Exception
FolderCopy = False
End Try
Else
FolderCopy = False
End If
End Function
在使用Imports System.IO
方法之前,请确保在课程开头包含FolderCopy
,并注意这两种方法都必须包含在内。
答案 1 :(得分:0)
' copy all files and subdirectories from the
' specified source to the specified destination.
Private Sub RecursiveCopyFiles( ByVal sourceDir As String, ByVal destDir As String, _
ByVal fRecursive As Boolean)
Dim i As Integer
Dim posSep As Integer
Dim sDir As String
Dim aDirs() As String
Dim sFile As String
Dim aFiles() As String
' Add trailing separators to the supplied paths if they don't exist.
If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
sourceDir &= System.IO.Path.DirectorySeparatorChar
End If
If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
destDir &= System.IO.Path.DirectorySeparatorChar
End If
' Recursive switch to continue drilling down into dir structure.
If fRecursive Then
' Get a list of directories from the current parent.
aDirs = System.IO.Directory.GetDirectories(sourceDir)
For i = 0 To aDirs.GetUpperBound(0)
' Get the position of the last separator in the current path.
posSep = aDirs(i).LastIndexOf("\")
' Get the path of the source directory.
sDir = aDirs(i).Substring((posSep + 1), aDirs(i).Length -(posSep + 1))
' Create the new directory in the destination directory.
System.IO.Directory.CreateDirectory(destDir + sDir)
' Since we are in recursive mode, copy the children also
RecursiveCopyFiles(aDirs(i), (destDir + sDir), fRecursive)
Next
End If
' Get the files from the current parent.
aFiles = System.IO.Directory.GetFiles(sourceDir)
' Copy all files.
For i = 0 To aFiles.GetUpperBound(0)
' Get the position of the trailing separator.
posSep = aFiles(i).LastIndexOf("\")
' Get the full path of the source file.
sFile = aFiles(i).Substring((posSep + 1), aFiles(i).Length - (posSep+ 1))
' Copy the file.
System.IO.File.Copy(aFiles(i), destDir + sFile)
Next i
End Sub