2010-11-04 76 views
0

當我的程序想要爲Win 7(或Vista)中的所有用戶添加啓動快捷方式時,它有一個「未經授權的訪問例外」,即使我以管理員身份登錄。獲得授權所有用戶的啓動快捷方式

如何在我的程序中爲所有用戶授予訪問權限?

下面是代碼:

Imports IWshRuntimeLibrary 

公共類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 

末級

+0

你嘗試了什麼,看到你的代碼可能會有所幫助。添加代碼 – 2010-11-04 03:46:22

+0

(請參閱上文)。 – jameslcs 2010-11-04 19:22:55

回答

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」添加爲環境變量的一部分,您嘗試查找該環境變量並返回爲空,未添加到找到的變量。進行更改後,程序將寫入啓動目錄。

0

我嘗試上面的方法,它在我的D:\中創建一個文件夾,不知道爲什麼?

無論如何,我發現它是我的程序需要處理的UAC(需要對應用程序清單文件進行一些調整)。

感謝您的幫助馬克。

相關問題