0

我已经为此工作了一段时间,这让我发疯,经过大量搜索和阅读仍然没有解决方案。我正在尝试使用“提升”(域管理员)权限启动一个进程。

这是我的代码:

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

我也尝试过模拟用户,但这似乎也不起作用。我一直在努力解决2个错误:

  1. 找不到指定的路径
  2. 拒绝访问

其他帖子指出,如果您使用 的userpassword参数,则ProcessInfo需要将 设置UseShellExecutefalse。如果您已将其设置为falseworkingDirectory则不再使用 。

有人有什么建议吗?

4

1 回答 1

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
    }
}
于 2013-10-22T18:05:47.410 回答