使用管理员凭据运行流程

时间:2013-10-22 17:59:02

标签: c#

我已经在这方面工作了一段时间,经过大量的搜索和阅读仍然没有解决方案,它让我疯狂。 我正在尝试使用“Elevated”(域管理员)权限启动流程。

这是我的代码:

public class StartProcess
{
    private ProcessStartInfo info;

    private SecureString ConvertPassword(string _password)
    {
        SecureString password = new SecureString();

        for (int i = 0; i < _password.Length; i++)
        {
            password.AppendChar(_password[i]);
        }

        return password;
    }

    public void Start(string path, string fileName)
    {
        info = new ProcessStartInfo(fileName);

        info.Domain = "setours";
        info.UserName = "administrator";
        info.Password = ConvertPassword("ServerTotal_2013");

        info.UseShellExecute = false;
        info.WorkingDirectory = path;

        try
        {
            Process.Start(info);
        }
        catch (System.ComponentModel.Win32Exception ex)
        {
            MessageBox.Show(ex.Message + ex.StackTrace);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + ex.StackTrace);
        }
        finally
        {
            //Just for debuggin purpuses
            MessageBox.Show(path + fileName);
        }
    }
}

我正在根据我的主要课程中的按钮点击事件调用该函数:

private StartProcess startProcess = new StartProcess();

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;

        switch (button.Content.ToString())
        {
            case "Contable":
                startProcess.Start("C:\\Program Files (x86)\\StarSoft GE", "\\Contabilidad.exe");
                break;
            case "Planilla":

                break;
            case "Caja y banco":

                break;
            case "Cuentas por pagar":

                break;
            default:
                MessageBox.Show("Unknown process");
                break;
        }
    }

我也尝试过用户模仿,但这似乎也不起作用。 我一直在犯两个错误:

  1. 找不到指定的路径
  2. 拒绝访问
  3. 其他帖子指出,如果您使用user的{​​{1}}和password参数,则需要将ProcessInfo设置为UseShellExecute。 如果您将其设置为false,则false将不再使用。

    有没有人有任何吸烟?

1 个答案:

答案 0 :(得分:1)

试试这个:

private void RunElevated(string fileName)
{
    ProcessStartInfo processInfo = new ProcessStartInfo();
    processInfo.Verb = "runas";
    processInfo.FileName = fileName;
    try
    {
        Process.Start(processInfo);
    }
    catch (Win32Exception)
    {
        //Do nothing. Probably the user canceled the UAC window
    }
}
相关问题