【问题标题】:VB.NET - Choose folder by browsing and copy file from application directoryVB.NET - 通过浏览选择文件夹并从应用程序目录复制文件
【发布时间】:2012-06-10 21:28:16
【问题描述】:

您好 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
  • SnoX

【问题讨论】:

    标签: vb.net copy directory


    【解决方案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 ) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 2014-03-27
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 1970-01-01
      • 2019-05-28
      相关资源
      最近更新 更多