How to copy file in the same directory to multi places?

Mostafa Salaheldien 61 Reputation points
2021-06-03T23:15:31.973+00:00

Hello, everyone,
I would like to copy a file from the same directory to multi places
this is my code I am using to copy one file to one place

My.Computer.FileSystem.CopyFile( _
"C:\UserFiles\TestFiles\test.txt", _
"C:\Users\TestFiles2\test.txt", overwrite:=False)

but I want to copy that file to the desktop for every user has a profile already on my pc

C:\Users\User1\Desktop
C:\Users\User2\Desktop
C:\Users\User3\Desktop
C:\Users\User4\Desktop
C:\Users\User5\Desktop
C:\Users\User6\Desktop
C:\Users\User7\Desktop
C:\Users\User8\Desktop
C:\Users\User9\Desktop
C:\Users\User10\Desktop
C:\Users\User11\Desktop

thanks in advances

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-04T00:27:10.493+00:00

    Hello,

    You can try this code:

    Dim Users As String() = System.IO.Directory.GetDirectories("C:\Users\")
    
            For i = 0 To Users.Length - 1
                Try
                    My.Computer.FileSystem.CopyFile("C:\UserFiles\TestFiles\test.txt", "C:\Users\" & Users(i) & "\Desktop\test.txt", overwrite:=False)
                Catch ex As Exception
                End Try
            Next
    

    This code will copy the file to the desktop for each user. I hope it helps.


1 additional answer

Sort by: Most helpful
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-06-04T02:41:20.38+00:00

    Hi @Mostafa Salaheldien ,
    You can consider copying the file to the public directory:

        c:\Users\Public\Desktop  
    

    For example:

    My.Computer.FileSystem.CopyFile("C:\UserFiles\TestFiles\test.txt", "C:\Users\Public\Desktop\test.txt", overwrite:=False)  
    

    Note: if your access is denied, try running visual studio as Administrator.
    Then every time you want to use the 'test. txt', you can :

    Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\test.txt"  
    ' do something with the file.  
    

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.