从 C# 以编程方式创建快捷方式并设置“以管理员身份运行”财产

发布于 2024-09-29 23:38:26 字数 322 浏览 1 评论 0原文

我已经知道如何使用 IWshRuntimeLibraryWshShellClass 从 C# 应用程序以编程方式创建快捷方式。或者我可以使用 IShellLink

现在,如果用户的电脑运行的是 Windows Vista 或 Windows 7,我希望能够以编程方式设置该快捷方式的“以管理员身份运行”属性。

这可能吗?如果是这样,怎么办?

alt text

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?

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你对谁都笑 2024-10-06 23:38:26

此示例使用 PowerShell,但使用与 C# 相同的对象和类。

使用以下代码获取要激活的字节号:

# 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])  
    } 
}

我得到字节号 21,其值为 34。
这是我用户的脚本:

# Turning on the byte of "Run as Admin"
$lnkBytes = [System.IO.File]::ReadAllBytes($shortCutLocation)
$lnkBytes[21] = 34
[System.IO.File]::WriteAllBytes($shortCutLocation, $lnkBytes)

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)
浅浅淡淡 2024-10-06 23:38:26

虽然 Doug 的答案是此问题的正确解决方案,但它不是此特定问题的答案...

要在 .lnk 上设置该属性,您需要使用 IShellLinkDataList COM 接口。伟大的 Raymond Chen 在他的博客上c++ 示例代码为此

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

放手` 2024-10-06 23:38:26

使用此方法,您可以创建一个设置了“以管理员身份运行”属性的快捷方式:
首先,您需要添加对“Windows Script Host Object Model”库的引用,它是一个COM库,因此在项目中,右键单击该引用转到COM部分,然后添加该库。

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);

    }
}

以管理员身份运行部分的功劳属于 此处

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

☆獨立☆ 2024-10-06 23:38:26

您需要为您的应用程序创建一个清单文件,以便让它请求以管理员权限运行。 这是一个不错的教程,您可以遵循。

尽情享受!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文