开发者

Gaining authorize Startup shortcut for All User

When my program wanted to add a startup shortcut for All User in Win 7 (or Vista), it got an "Unauthorized Access Exception",开发者_如何学Go even i login as admin.

How do get authorize access for All User in my program?

Here is the code:

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

End Class


You need to change

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

To

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

Note the difference you have the string "\Microsoft\Windows\Start Menu\Programs\Startup" added as part of the environment variable you are trying to find and was coming back as nothing, not added to the found variable. After making the change your program will write to the startup directory.


i try the above and it create a folder in my D:\, don't know why?

Anyway i found out it is the UAC that my program need to deal with (some adjustment on Application Manifest File are needed).

thanks for your help Mark.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜