简体   繁体   中英

How to create a shortcut without admin privileges in C#?

I want to create a .lnk shortcut to a .exe file on desktop but I can't run my program as admin.

Using Windows Script Host Object does not work for me, because I get the Task could not find “AxImp.exe” error. This problem could be solved by installing Windows SDK, but that also needs admin rights to be installed.

Is there a way in C# to create a .lnk shortcut without admin rights (whether third party or build-in doesn't matter)?

You can only create a shortcut on the desktop for yourself (ie the program is running under your account and it's trying to access your desktop). If you want to access anyone else's desktop, you need admin permissions. That's how Windows work and no matter whether you use C# or any other language.

If you want to create shortcut in current users's desktop and have UAC problem and cant't run as administrator , you can create a script.vbs in your project and set it to copy in output directory, then run vbs when you need.

Content of script.vbs:

Dim WshShell
Set WshShell = CreateObject("Wscript.shell")
Dim strDesktop 
strDesktop= WshShell.SpecialFolders("Desktop")
Dim oMyShortcut
Set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Shortcut to Notepad.lnk")
OMyShortcut.TargetPath = "notepad.exe"
oMyShortCut.Save
Set WshShell= Nothing
Set oMyShortcut= Nothing

Run script.vbs:

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start(System.IO.Path.Combine(Application.StartupPath, "script.vbs"));
}

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