简体   繁体   English

将程序移动到某个位置

[英]Moving Program to A Location

How could I move my VB.NET application to a folder using VB.NET in the same file?如何将我的 VB.NET 应用程序移动到在同一文件中使用 VB.NET 的文件夹中?

Essentially, I want to be able to move my VB.NET application to the Start folder when it is executed (within the same application).本质上,我希望能够在执行 VB.NET 应用程序时将其移动到 Start 文件夹(在同一应用程序中)。 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.另外,我希望能够将它移动到任何计算机上的同一文件夹,因为在不同计算机上没有设置 Start 文件夹的路径。 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首先,添加参考 -> COM 选项卡 -> Windows 脚本宿主 Object ZA559B87068921EEC05ZE086CE578

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 .因此,当您运行程序时,您不能移动它,而是必须创建某种bootstrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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