如何通过VB.net将文件从桌面文件夹复制到USB

时间:2016-11-02 04:27:33

标签: vb.net windows-7

我的商店有3台PC 3台Windows 7和2台Windows XP。每台PC都由不同的用户使用他们的帐户操作(帐户名称不同)。每个PC桌面上都有一个名为“papers”(唯一)的主文件夹。文件夹“papers”也包含子文件夹和文件。当我从USB的根目录点击应用程序时,我想将该文件夹(文件)复制到我的USB记忆棒。

User 1 : C:\Users\Peter\Desktop\papers\
User 2 : C:\Users\Ruwan\Desktop\papers\
User 3 : C:\Users\Sam\Desktop\papers\
User 4 : C:\Users\Roshy\Desktop\papers\
User 5 : C:\Users\Veronica\Desktop\papers\

Here the user's name is change to different users.

在编码中,我将FileToCopy的值指定为单个文件(Java_OOPs.docx)。我想查找FileToCopy值,将整个“papers”文件夹复制到USB。

我的问题是如何将文件夹(文件)和子文件夹复制到USB以及桌面文件夹的常用名称(因为用户姓名正在更改)(访问桌面“文件夹”文件夹)

注意:此应用程序也可以在Windows XP上运行。

Public Class Form1

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

    Dim Gig As Long = 1073741824
    Dim FileToCopy As String = "C:\Users\Peter\Desktop\papers\Java_OOPs.docx"

    Try
        For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
            If drive.DriveType = IO.DriveType.Removable AndAlso drive.IsReady AndAlso drive.AvailableFreeSpace >= 2 * Gig Then

                Dim DriveLetter As String = drive.Name
                Dim PathToUSBDrive = DriveLetter & IO.Path.GetFileName(FileToCopy)
                IO.File.Copy(FileToCopy, PathToUSBDrive)

            End If
        Next
    Catch ex As Exception
    End Try

End Sub

End Class

1 个答案:

答案 0 :(得分:2)

Public Class Form1

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

        Dim Gig As Long = 1073741824L
        Dim FolderToCopy As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "papers")

        Try
            For Each Drive As IO.DriveInfo In IO.DriveInfo.GetDrives
                Select Case True
                    Case Not Drive.DriveType = IO.DriveType.Removable
                    Case Not Drive.IsReady
                    Case Not Drive.AvailableFreeSpace >= 2 * Gig
                    Case Else
                        Debug.Print("Copying {0} to {1}", FolderToCopy, Drive.RootDirectory.FullName)
                        My.Computer.FileSystem.CopyDirectory(FolderToCopy, Drive.RootDirectory.FullName, True)

                        ' Only do the copy once to first appropriate drive
                        Exit For
                End Select
            Next
        Catch ex As Exception
            Debug.Print(ex.ToString)
        End Try

    End Sub

End Class