Reputation: 9
I am not very skilled in VB. I would like to keep this simple code. I want to use this code for another computer or user. How can I replace my usermane "John" in the code?
I tried replace it with %userprofile%
and similar words but it´s not working.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.CopyFile(
"C:\Users\John\Desktop\WindowsApp.exe",
"C:\Users\John\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WindowsApp.exe")
Upvotes: 0
Views: 472
Reputation: 32248
You can use the Environment.ExpandEnvironmentVariables method:
Dim userProfileBaseDir As String = Environment.ExpandEnvironmentVariables("%userprofile%")
This returns:
C:\Users\[CurrentUser]
Your code could be:
My.Computer.FileSystem.CopyFile(
$"{userProfileBaseDir}\Desktop\WindowsApp.exe",
$"{userProfileBaseDir}\AppData\Roaming\Microsoft\Windows\StartMenu\Programs\Startup\WindowsApp.exe")
But, since both Desktop
and StartMenu
are included in the Environment.SpecialFolder
enumerator, see: Environment.SpecialFolder enumeration
The Environment.GetFolderPath() method converts the enumeration to a Path.
Using interpolated strings:
Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim menuStart As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
My.Computer.FileSystem.CopyFile($"{desktopPath}\WindowsApp.exe", $"{menuStart}\WindowsApp.exe")
Or using Path.Combine to build a path:
Dim sourcePath = Path.Combine(desktopPath, "WindowsApp.exe")
Dim destPath = Path.Combine(menuStart, "WindowsApp.exe")
FileSystem.CopyFile(sourcePath, destPath)
Upvotes: 2
Reputation: 3632
If you need a quick and ready-to-use code, you can try this:
Dim userProfileDir As String
userProfileDir = Environ("USERPROFILE")
If your user is called IT
, MsgBox userProfileDir
would show this:
In your code it would be somenthing like this:
Dim sourcePath As String 'C:\User\MyUser
Dim destinationPath As String 'C:\User\MyUser\AppData\Roaming
sourcePath = Environ("USERPROFILE") & "\Desktop\WindowsApp.exe"
destinationPath = Environ("APPDATA") & "\Microsoft\Windows\Start Menu\Programs\Startup\WindowsApp.exe"
FileSystem.CopyFile(sourcePath, destinationPath)
Hope this helps.
Upvotes: 0
Reputation: 605
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.CopyFile( "C:\Users\" & Environ("USERNAME") & "\Desktop\WindowsApp.exe", "C:\Users\" & Environ("USERNAME") & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WindowsApp.exe")
Upvotes: 0
Reputation: 15774
Use the Environment.SpecialFolder
Enum for both paths. And use Path.Combine
to build paths.
My.Computer.FileSystem.CopyFile(
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "WindowsApp.exe"),
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "WindowsApp.exe"))
Perhaps WindowsApp.exe
is the name of your executing application. Now you have no hard-coding
My.Computer.FileSystem.CopyFile(
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), System.AppDomain.CurrentDomain.FriendlyName),
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), System.AppDomain.CurrentDomain.FriendlyName))
Or you want to copy the currently running application to startup
My.Computer.FileSystem.CopyFile(
Application.ExecutablePath,
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), System.AppDomain.CurrentDomain.FriendlyName))
Upvotes: 1