C#在计划任务中使用不同用户启动进程

时间:2016-10-11 09:13:50

标签: c# process scheduled-tasks processstartinfo

我在Windows任务调度程序上运行应用程序,我需要使用不同的用户从此应用程序调用另一个exe。我尝试了以下但它永远不会得到异常或调用exe。是什么错误?

void callExe()
    {
        try
        {
            ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
            cmdStartInfo.FileName = @"C:\Batch\ProcessTest.exe";
            cmdStartInfo.WorkingDirectory = @"C:\Windows\System32";
            cmdStartInfo.RedirectStandardOutput = true;
            cmdStartInfo.RedirectStandardError = true;
            cmdStartInfo.RedirectStandardInput = true;
            cmdStartInfo.UseShellExecute = false;

            cmdStartInfo.Domain = "xxxxx";
            cmdStartInfo.UserName = "xxx";
            var s = new SecureString();
            s.AppendChar('1');
            s.AppendChar('2');
            s.AppendChar('3');
            s.AppendChar('4');
            s.AppendChar('5');
            cmdStartInfo.Password = s;

            Process proc = new Process();
            proc.StartInfo = cmdStartInfo;
            proc.ErrorDataReceived += proc_ErrorDataReceived; // nothing received
            proc.OutputDataReceived += proc_OutputDataReceived; // nothing received
            proc.EnableRaisingEvents = true;

            proc.Start();
            proc.BeginOutputReadLine();
            proc.BeginErrorReadLine();

            proc.StandardInput.WriteLine("exit");
            proc.WaitForExit();
            proc.Close();
        }
        catch (Win32Exception w32E) // never catches exception
        {
            Console.WriteLine("w32 error: " + w32E);
        }
        catch (Exception ex)
        {
            Console.WriteLine("error: " + ex.Message);
        }
        return;
    }


    void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine("output: ");
        Console.WriteLine(e.Data);
    }

    void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine("error: ");
        Console.WriteLine(e.Data);
    }

0 个答案:

没有答案