以管理员身份将 C# 中的进程设为 运行

Making Processes inside C# to run as administrator

我在 CMD 中使用 attrib 命令制作了一个删除病毒快捷方式的应用程序,我需要管理员权限才能执行命令。

目前我无法通过整个程序或进程本身来做到这一点。 这是一段需要 UAC 的代码:

private void Button2_Click(object sender, EventArgs e)
{
    string Disktype = null;
    Disktype = ListBox1.GetItemText(ListBox1.SelectedItem); ;
    if (Disktype.Contains("C:") == true)
    {
        MessageBox.Show("You selected C:");
    }
    else
    {
        String Message = "Cleaning" + Disk.Diskvolume + "Are you Sure?";
        String Title = "Cleaning now";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result = MessageBox.Show(Message, Title, buttons);
        if (result == DialogResult.Yes)
        {
            String ShowFiles = "attrib -h -r -s /s /d" + Disktype + "*.*";
            String RemoveVirus = "del" + Disktype + "*.lnk";
            System.Diagnostics.Process.Start("CMD.exe", ShowFiles);
            System.Diagnostics.Process.Start("CMD.exe", RemoveVirus);
            MessageBox.Show("Cleaning");
            System.Diagnostics.Process.Start(Disktype);
            Close();
        }
        else
        {
            MessageBox.Show("Cleaning done");
            Close();
        }
    }

如您所见,我直接将 ShowFilesRemoveVirus 设置为 cmd,但我在如何以管理员身份设置 运行 方面遇到了麻烦。

整个 C# 应用程序还是仅 Process.Start 个应用程序都需要它?

我在找,你可以试试这个:

string command;
command = "powershell -Command 'Start-Process cmd -Verb RunAs'"
System.Diagnostics.Process.Start("cmd.exe",command);

您可以使用 powershell 以管理员身份启动 CMD

使用它您可以毫无问题地使用命令。

希望对你有用:)