-2

你好 StackOverflow 用户。我正在尝试创建一个应用程序,您可以在其中浏览到文件夹,按安装按钮,它会将一些文件复制到您选择的目录中?我找到了一些示例代码,但我如何从这里继续我的代码?无法弄清楚如何复制文件。您终于可以在代码中看到我试图复制文件但它并没有真正起作用,我该如何使用该功能?我希望文件来自应用程序目录。并复制到浏览的文件夹。

Public Class Installer

Private Sub Installer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked




End Sub




Private Sub BrowseButton_Click_1(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
    ' Declare a variable named theFolderBrowser of type FolderBrowserDialog.
    Dim theFolderBrowser As New FolderBrowserDialog

    ' Set theFolderBrowser object's Description property to
    '   give the user instructions.
    theFolderBrowser.Description = "Please browse to your GTAIV directory."

    ' Set theFolderBrowser object's ShowNewFolder property to false when
    '   the a FolderBrowserDialog is to be used only for selecting an existing folder.
    theFolderBrowser.ShowNewFolderButton = False

    ' Optionally set the RootFolder and SelectedPath properties to
    '   control which folder will be selected when browsing begings
    '   and to make it the selected folder.
    ' For this example start browsing in the Desktop folder.
    theFolderBrowser.RootFolder = System.Environment.SpecialFolder.Desktop
    ' Default theFolderBrowserDialog object's SelectedPath property to the path to the Desktop folder.
    theFolderBrowser.SelectedPath = My.Computer.FileSystem.SpecialDirectories.Desktop

    ' If the user clicks theFolderBrowser's OK button..
    If theFolderBrowser.ShowDialog = Windows.Forms.DialogResult.OK Then
        ' Set the FolderChoiceTextBox's Text to theFolderBrowserDialog's
        '    SelectedPath property.
        Me.FolderTextBox.Text = theFolderBrowser.SelectedPath

    End If
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles InstallButton.Click

    My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath, theFolderBrowser.SelectedPath)


End Sub
End Class
  • 斯诺克斯
4

1 回答 1

1

当你调用这行代码时

My.Computer.FileSystem.CopyFile(My.Application.Info.DirectoryPath,  _
                                theFolderBrowser.SelectedPath) 

theFolderBrowser无法引用该对象,因为它是另一个方法中的局部变量。但是,在退出之前,您已将所选路径复制到文本框中。您可以将该文本框用作副本的目的地

My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath,  _
                                     Me.FolderTextBox.Text ) 

还要记住,CopyFile 只复制一个文件,如果您打算复制整个文件夹,则需要 CopyDirectory 方法。这个方法还有一个重要的细节:

如果您的文件存在于目标目录中并且您没有使用带有覆盖标志的重载,您将得到一个异常

My.Computer.FileSystem.CopyDirectory(My.Application.Info.DirectoryPath,  _
                                     Me.FolderTextBox.Text, True ) 
于 2012-06-06T20:13:29.040 回答