0

VB.net を使用して、ある場所から別の場所に HTML ファイルをコピーしたいと考えています。
3 つの FileCopy、System.IO.File.Copy、My.Computer.FileSystem.CopyFile のいずれかを使用すると、ファイルのみがコピーされ、関連するイメージとスクリプトを含む「filename_files」フォルダーはコピーされません。

私がプログラムでやりたいことは、a.htmlをb.htmlとして別の場所にコピーすることです

それを行って b.html を開くと、画像やスクリプトなしで開きます。

助けてください

4

2 に答える 2

1

スクリプトと画像を含むフォルダーをまとめてコピーできる次の 2 つの方法を使用できます。したがって、組み込みの方法でFileCopyHTML ファイルをコピーし、以下の方法を使用して必要なフォルダーをコピーします。

ここで、特定のパスにあるファイルの配列を返す最初のメソッドを見つけました

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

上記の方法と以下の方法を使用して、ソースとターゲットのパスを指定してフォルダーをコピーできます。このメソッドは、フォルダがコピーされたかどうかを示すブール値を返します。

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含めてください。これらのメソッドは両方とも含める必要があることに注意してください。

于 2011-03-22T13:39:12.803 に答える
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
于 2011-03-24T05:37:37.613 に答える