-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 は 1 つのファイルのみをコピーすることに注意してください。フォルダー全体をコピーする場合は、CopyDirectory メソッドが必要です。この方法には、もう 1 つの重要な詳細があります。

ファイルが宛先ディレクトリに存在し、オーバーライト フラグを指定してオーバーロードを使用しない場合、例外が発生します。

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