开发者

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 开发者_Go百科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?

Create shortcuts programmatically from C# and set "Run as administrator" property


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 activate:

# Find the missing admin byte (use this code, when changing the link):
$adminon = [System.IO.File]::ReadAllBytes($shortCutLocation)
$adminoff = [System.IO.File]::ReadAllBytes($shortCutLocation)
for ($i = 0; $i -lt $adminon.Count; $i++) { 
    if ($adminon[$i] -ne $adminoff[$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)


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


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


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!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜