Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.NET
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:

VB
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
Posted
Updated 2-Nov-16 5:01am

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.

VB.NET
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)
 
Share this answer
 
Comments
S.SAKTHYBAALAN 3-Nov-16 9:43am    
I got error Error 'CommonDesktopDirectory' is not a member of 'System.Environment.SpecialFolder'.
Richard Deeming 3-Nov-16 9:47am    
Then you're using .NET 3.5 or earlier. That member was added in .NET 4.0, so you'll either need to upgrade, or remove that If block.
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.
 
Share this answer
 
v3
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 ...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900