'Create shortcuts programmatically from C# and set "Run as administrator" property
I already know how to create shortcuts programmatically from my C# applications using IWshRuntimeLibrary and WshShellClass. Or I could use IShellLink.
Now, if the user's PC is running Windows Vista or Windows 7, I would like to be able to set the "Run as administrator" property of that shortcut programmactically as well.
Is that possible? If so, how?

Solution 1:[1]
While Doug's answer is the correct solution to this problem, it is not the answer to this specific question...
To set that property on a .lnk, you need to use the IShellLinkDataList COM interface. The great Raymond Chen has c++ sample code on his blog for this
Solution 2:[2]
This example is in PowerShell, but is uses the same objects and classes as C#.
Use the following code to get the byte number to activtae:
# Find the missing admin byte (use this code, when changing the link):
$adminon = [System.IO.File]::ReadAllBytes($shortCutLocation)
$adminof = [System.IO.File]::ReadAllBytes($shortCutLocation)
for ($i = 0; $i -lt $adminon.Count; $i++) {
if ($adminon[$i] -ne $adminof[$i]) {
Write-Host Location: $i Value: $($adminon[$i])
}
}
I got byte number 21 and its value was 34. So this is the script I user:
# Turning on the byte of "Run as Admin"
$lnkBytes = [System.IO.File]::ReadAllBytes($shortCutLocation)
$lnkBytes[21] = 34
[System.IO.File]::WriteAllBytes($shortCutLocation, $lnkBytes)
Solution 3:[3]
You will need to create a manifest file for your application in order to get it to request run as an administrator privileges. Here is a nice tutorial you can follow.
Enjoy!
Solution 4:[4]
With this method you can create a shortcut that its “Run as administrator” property is set: first, you need to add a reference to the "Windows Script Host Object Model" library, it is a COM library, so in the project, right-click on the reference go to the COM section, and add the library.
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using IWshRuntimeLibrary;
class Solution
{
static void CreateShortcut(string shortcutPath, string sourcePath, bool runAsAdmin, params string[] args)
{
var shortcut = new IWshShell_Class().CreateShortcut(shortcutPath) as IWshShortcut;
shortcut.TargetPath = System.IO.Path.GetFullPath(sourcePath);
shortcut.Arguments = "\"" + string.Join("\" \"", args) + "\"";
shortcut.Save();
if (runAsAdmin)
using (var fs = new FileStream(shortcutPath, FileMode.Open, FileAccess.ReadWrite))
{
fs.Seek(21, SeekOrigin.Begin);
fs.WriteByte(0x22);
}
}
static void Main(string[] args)
{
CreateShortcut(Directory.GetCurrentDirectory() + "\\" + "shortcutName" + ".lnk", @"C:\...... path to file ... .exe", true);
}
}
Credit for run as admin section belongs to here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anders |
| Solution 2 | Mor Shemesh |
| Solution 3 | Doug |
| Solution 4 |
