代码之家  ›  专栏  ›  技术社区  ›  JohnB

从C以编程方式创建快捷方式,并设置“以管理员身份运行”属性

  •  3
  • JohnB  · 技术社区  · 16 年前

    IWshRuntimeLibrary 和 WshShellClass . 或者我可以用 IShellLink

    现在,如果用户的PC运行的是WindowsVista或Windows7,我希望能够设置 “以管理员身份运行”

    有可能吗?如果是,怎么做?

    alt text

    4 回复  |  直到 16 年前
        1
  •  5
  •   Anders    16 年前

    虽然道格的答案是这个问题的正确答案,但不是这个具体问题的答案。。。

    要在.lnk上设置该属性,需要使用 IShellLinkDataList c++ sample code on his blog 为了这个

        2
  •  3
  •   Doug    16 年前

    你需要 创建清单文件 以便使应用程序请求以管理员权限运行。 Here is a nice tutorial you can follow.

    享受吧!

        3
  •  3
  •   Mor Shemesh    12 年前

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

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

    所以这是我的脚本用户:

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

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

    作为管理部分运行的学分属于 here