如何使用VB.NET从任何桌面复制所有内容(文件和文件夹) [英] How do I copy all contents (files and folders) from any desktop using VB.NET

查看:114
本文介绍了如何使用VB.NET从任何桌面复制所有内容(文件和文件夹)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

用户 1 :C:\ Users \ Peter \Desktop \papers \
用户 2 :C:\ Users \Ruwan \\ \\Desktop\papers \
用户 3 :C:\ Users \Sam \Desktop \papers \
用户< span class =code-digit> 4 :C:\ Users\Roshy \Desktop \papers \
用户 5 :C:\ Users \ Veronica \Desktop\papers \

这里用户' s name是对不同用户的更改。
编码中,我分配了值 for FileToCopy as a single 文件(Java_OOP s.docx)。我希望 chage FileToCopy值, 复制整个 论文文件夹 USB。

我的问题 如何复制文件夹(论文) sub 文件夹 USB 一个通用名称(因为用户' 名称正在更改)文件夹(访问桌面papers文件夹)

注意:此应用程序可以 运行 on Windows XP。





我尝试过:



 公共  Form1 

私有 Sub Form1_Load( ByVal sender As System。 Object ByVal e As System.EventArgs)句柄 MyBase .Load

Dim Gig As = 1073741824
Dim FileToCopy As String =& QUOT; C:\Users\Peter\Desktop\papers\Java_OOPs.docx&安培; QUOT;

尝试
对于 每个驱动器 As System.IO.DriveInfo System.IO.DriveInfo.GetDrives
如果 drive.DriveType = IO.DriveType.Removable AndAlso drive.IsReady AndAlso drive.AvailableFreeSpace& gt; = 2 * Gig 然后

Dim DriveLetter 作为 字符串 = drive.Name
Dim PathToUSBDrive = DriveLetter& amp; IO.Path.GetFileName(FileToCopy)
IO.File.Copy(FileToCopy,PathToUSBDrive)

结束 如果
下一步
Catch ex < span class =code-keyword> As 异常
结束 尝试

结束 Sub

结束

解决方案

To检索当前用户的桌面文件夹,您需要使用环境。 GetFolderPath [ ^ ]方法。



des ktop实际上是两个文件夹的组合 - 共享桌面文件夹和用户的桌面文件夹。根据您的代码,您看到的文件看起来像是存储在用户的桌面文件夹中,但您可能需要检查两者以防万一。



< pre lang =VB.NET> Dim desktopPath 作为 String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim papersPath As 字符串 = Path.Combine(desktopPath, papers

如果 Directory.Exists(papersPath)< span class =code-keyword>然后
desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
papersPath = Path.Combine(desktopPath, 论文)
如果 Directory.Exists(papersPath)然后
MessageBox.Show( 找不到Papers文件夹。
返回
结束 如果
结束 如果

获取相对于应用程序的目标路径:
Dim usbPath As String = Path.GetPathRoot(Application.ExecutablePath)
FileSystem。 CopyDirectory(papersPath,usbPath)


所以你想要同步每个文件夹。如果在多个地方修改了同一个文件,会发生什么?你会保留哪个版本?



以这种方式处理共享文件似乎是一个坏主意。迟早,你可能会遇到数据丢失问题。



我会:



- 在一个地方建立文件(其中一台计算机,我们将在下面称为'服务器');



- 在此文件夹上创建一个共享;

共享文件夹或驱动器 - MS TechNet [ ^ ]



- 在服务器上创建所有用户帐户(在每台计算机上使用);帐户必须有匹配的用户名;密码匹配并不是严格要求,但可以让生活更轻松。

在Windows中创建用户帐户 [ ^ ]



- 在远程计算机上,在公共桌面上创建一个快捷方式,指向服务器上的共享。



希望它有意义。相信我,手动同步解决方案将很快成为一场噩梦。

如果计算机不在网络上,设置计算机并不困难也不昂贵。



亲切。


也许您应该尝试使用以下内容:

FileSystem.CopyDirectory: https ://msdn.microsoft.com/de-de/library/ms127957.aspx



但你也应该看看你对PathToUSBDrive的任务它的内容......



对于你问题的其他部分:

这取决于安装的.Net-Framework目标系统,也是您编写应用程序的框架......


There are 5 PCs in my shop 3 windows 7 and 2 windows XP. Every PC is operated by different users with their account (Account names are different). A main folder called "papers" (unique) is in every PC's desktop. The folder "papers" contains subfolders and files also. I want to copy that folder(papers) to my USB stick when I clicked the application from the root of the 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.
In the coding, I assigned the value for FileToCopy as a single file (Java_OOPs.docx). I want to chage the FileToCopy value, to copy the entire "papers" folder to the USB.

My question is how to copy the folder (papers) and sub folder to the USB and a common name (Because the user's name is changing) for the desktop folder(access the desktop "papers" folder)

Note : This application may able to run on Windows XP too.



What I have tried:

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 = &quot;C:\Users\Peter\Desktop\papers\Java_OOPs.docx&quot;

    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 &gt;= 2 * Gig Then

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

            End If
        Next
    Catch ex As Exception
    End Try

End Sub

End Class

解决方案

To retrieve the desktop folder for the current user, you will need to use the Environment.GetFolderPath[^] method.

The desktop is actually a combination of two folders - the shared desktop folder, and the user's desktop folder. Based on your code, it looks like the files you're after are stored in the user's desktop folder, but you might need to check both just in case.

Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
Dim papersPath As String = Path.Combine(desktopPath, "papers")

If Not Directory.Exists(papersPath) Then
    desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)
    papersPath = Path.Combine(desktopPath, "papers")
    If Not Directory.Exists(papersPath) Then
        MessageBox.Show("Papers folder not found.")
        Return
    End If
End If

' Get the destination path relative to the application:
Dim usbPath As String = Path.GetPathRoot(Application.ExecutablePath)
FileSystem.CopyDirectory(papersPath, usbPath)


So you want to synchronize each folder. What will happen if the same file has been modified at several places? Which version will you keep?

It seems a bad idea to handle shared files this way. Sooner or later, you will likely encounter a data-loss issue.

I would:

- establish the files in one place (one of the computers, that we will call 'server' below);

- create a share on this folder;
Share a Folder or Drive - MS TechNet[^]

- create all user accounts (which are used on each computer) on the server; accounts must have matching username; password matching is not strictly required, but it can make life easier.
Create a user account in Windows[^]

- on the remote computers, create a shortcut, on the public desktop, pointing to the share on the server.

Hope it makes sense. believe me, the manual synchronization solution will become a nightmare very quickly.
If computers are not on a network, it is not really hard nor expensive to set one up.

Kindly.


Perhaps you should try to use the following :
FileSystem.CopyDirectory : https://msdn.microsoft.com/de-de/library/ms127957.aspx

But also you should take a look to your assignment to PathToUSBDrive and it's Content ...

For the other part of your question :
It depends on the .Net-Framework which is installed on the destination-system andalso the Framework for which you have written your application ...


这篇关于如何使用VB.NET从任何桌面复制所有内容(文件和文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆