简体   繁体   中英

Moving Program to A Location

How could I move my VB.NET application to a folder using VB.NET in the same file?

Essentially, I want to be able to move my VB.NET application to the Start folder when it is executed (within the same application). Also, I want to be able to move it to the same folder on any computer, for there is no set path to the Start folder on different computers. Or I would like to just create a shortcut for my application in the start folder

Thanks,

Odinulf

The following code will create a shortcut to the running application in the Start Menu (you will need the necessary permissions to do this):

Firstly, Add Reference -> COM tab -> Windows Script Host Object Model

Imports IWshRuntimeLibrary

Public Function CreateShortcut(ByVal fileName As String, ByVal description As String) As Boolean
        Try
            Dim shell As New WshShell
            'the following is the root of the Start Menu so if you want it in a folder you need to create it
            Dim ShortcutDir As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)
            Dim shortCut As IWshRuntimeLibrary.IWshShortcut

            ' short cut files have a .lnk extension
            shortCut = CType(shell.CreateShortcut(ShortcutDir & "\" & fileName & ".lnk"), IWshRuntimeLibrary.IWshShortcut)

            ' set the shortcut properties
            With shortCut
                .TargetPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
                .WindowStyle = 1
                .Description = description
                .WorkingDirectory = ShortcutDir
                ' the next line gets the first Icon from the executing program
                .IconLocation = System.Reflection.Assembly.GetExecutingAssembly.Location() & ", 0"

                '.Arguments = ""
                .Save()
            End With
            Return True
        Catch ex As System.Exception
            ' add your error handling here
            Return False
        End Try
    End Function

You cannot move, rename or delete a process that is currently in use. So when you are running your program you can't move it, instead you'll have to create some kind of bootstrapper .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM