0

I'm running an application on windows task scheduler, I need to call another exe from this app with different user. I've tried the following but it never gets exception or calls the exe. Whats the mistake?

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);
    }
Kemal Duran
  • 1,478
  • 1
  • 13
  • 19

0 Answers0