获取授权所有用户的启动快捷方式

时间:2010-11-04 03:41:28

标签: vb.net

当我的程序想要在Win 7(或Vista)中为所有用户添加启动快捷方式时,它会出现“未经授权的访问异常”,即使我以管理员身份登录。

如何在我的程序中授权所有用户访问?

以下是代码:

Imports IWshRuntimeLibrary

Public Class Form1

Dim AppName As String = "StartUp ShortCut"
Dim startUpFolderPathALLUSERfWin7 As String = Environment.GetEnvironmentVariable(("ALLUSERSPROFILE") & "\Microsoft\Windows\Start Menu\Programs\Startup")

Private Sub Create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim lnkPathAllUserWin7 As String = startUpFolderPathALLUSERfWin7 & "\" & AppName & ".lnk" 'need permission
    Dim appPath As String = My.Computer.FileSystem.CurrentDirectory & "\" & AppName & ".exe"

    Try
        Dim wshs As IWshShell_Class = New IWshShell_Class
        Dim shortcut As IWshShortcut_Class = TryCast(wshs.CreateShortcut(lnkPathAllUserWin7), IWshShortcut_Class)
        shortcut.Description = "This is a shortcut to " & AppName
        shortcut.TargetPath = appPath
        shortcut.IconLocation = appPath + ",0"
        shortcut.Save()
        MsgBox("ShortCut File Created")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

结束班

2 个答案:

答案 0 :(得分:0)

您需要更改

Dim startUpFolderPathALLUSERfWin7 As String = Environment.GetEnvironmentVariable(("ALLUSERSPROFILE") & "\Microsoft\Windows\Start Menu\Programs\Startup")

Dim startUpFolderPathALLUSERfWin7 As String = Environment.GetEnvironmentVariable("ALLUSERSPROFILE") & "\Microsoft\Windows\Start Menu\Programs\Startup"

请注意,您将字符串“\ Microsoft \ Windows \ Start Menu \ Programs \ Startup”作为您尝试查找的环境变量的一部分添加,并且没有添加到找到的变量中。进行更改后,程序将写入启动目录。

答案 1 :(得分:0)

我尝试以上操作并在我的D:\中创建一个文件夹,不知道为什么?

无论如何,我发现我的程序需要处理的是UAC(需要对Application Manifest File进行一些调整)。

感谢您的帮助Mark。