C# 在windows窗体中启动psexec进程失败

C# 在windows窗体中启动psexec进程失败,c#,C#,因此,我创建了一个表单,允许我们的营销团队重新启动一台计算机,为访客显示幻灯片/重要信息等 计划是将其部署到其他用户编译器,然后他们可以执行以下三项任务之一: 现在重启 定时/定时重启 取消定时/计划重新启动 该应用程序在我的计算机上运行良好,只是在调试和构建中运行良好。 当安装在其他人的计算机上时,它似乎正在启动psexec,但却什么也不做 我在中添加了一些catch异常,此时它会显示以下错误: standardout尚未重定向 然而,在研究这个问题时,我似乎已经有了大多数人建议的解决这个问题

因此,我创建了一个表单,允许我们的营销团队重新启动一台计算机,为访客显示幻灯片/重要信息等

计划是将其部署到其他用户编译器,然后他们可以执行以下三项任务之一: 现在重启 定时/定时重启 取消定时/计划重新启动

该应用程序在我的计算机上运行良好,只是在调试和构建中运行良好。 当安装在其他人的计算机上时,它似乎正在启动psexec,但却什么也不做

我在中添加了一些catch异常,此时它会显示以下错误:

standardout尚未重定向

然而,在研究这个问题时,我似乎已经有了大多数人建议的解决这个问题的代码

我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace Media_Wall_Restart_Scheduler
{

public partial class frm_mediawall_startform : Form
{

    public frm_mediawall_startform()
    {
        InitializeComponent();
        input_time.Value = DateTime.Now;


    }
    static class Programmability
    {
        /* Secure Credential Store - This is for remote reboots */
        public static readonly string mediawallcomputername = @"\\computername";
        public static readonly string adminusername = "computeraccount";
        public static readonly string admindomainame = "domainname";
        public static readonly string adminpassword = "computeraccountpassword";
        public static readonly string addadminpassword = " -p " + adminpassword;
        public static readonly string adminuseraccount = admindomainame + @"\" + adminusername;
        public static readonly string addadminuseraccount = " -u " + admindomainame + @"\" + adminusername;
        public static readonly string Restartnow = mediawallcomputername + addadminuseraccount + addadminpassword + " shutdown -r -t 30";
        public static readonly string CancelShutdown = mediawallcomputername + addadminuseraccount + addadminpassword + " shutdown -a";
        public static readonly string psexecpath = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "Resources", "PsExec.exe"));
        /* End Secure credential Store  */
    }

    private void Btn_restart_now_Click(object sender, EventArgs e)
    {
        /* Quick and simple Restart now command */
        Process Restart_now = new Process();
        Restart_now.StartInfo.UseShellExecute = false;
        Restart_now.StartInfo.RedirectStandardOutput = true;
        Restart_now.StartInfo.RedirectStandardError = true;
        Restart_now.StartInfo.RedirectStandardInput = true;
        Restart_now.StartInfo.FileName = Programmability.psexecpath;
        var Restartnow_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -s -t 30" };
        Restart_now.StartInfo.Arguments = string.Join(" ", Restartnow_args);
        {
            Restart_now.Start();
            string Output = Restart_now.StandardOutput.ReadToEnd();
            string Errormessage = Restart_now.StandardError.ReadToEnd();
            Restart_now.WaitForExit();

        }



    }

    public void Btn_scheduled_restart_Click(object sender, EventArgs e)
    {
        /* Gets the current time */
        DateTime Timenow = DateTime.Now;
        DateTime Restarttime = input_time.Value;
        /* Takes the Scheduled time and subtracts from now time above */
        var ScheduledRestartTime = Math.Ceiling((Restarttime - Timenow).TotalSeconds);
        /* Run the command to restart the Media wall at the specifed time */
        Process Timed_Restart = new Process();
        Timed_Restart.StartInfo.UseShellExecute = false;
        Timed_Restart.StartInfo.RedirectStandardOutput = true;
        Timed_Restart.StartInfo.RedirectStandardError = true;
        Timed_Restart.StartInfo.RedirectStandardInput = true;
        Timed_Restart.StartInfo.FileName = Programmability.psexecpath;
        var TimedRestart_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -r", $"-t {ScheduledRestartTime}" };
        Timed_Restart.StartInfo.Arguments = string.Join(" ", TimedRestart_args);
        {
            Timed_Restart.Start();
            string Output = Timed_Restart.StandardOutput.ReadToEnd();
            string Errormessage = Timed_Restart.StandardError.ReadToEnd();
            Timed_Restart.WaitForExit();
        }


    }



    private void Btn_stop_restart_Click(object sender, EventArgs e)
    {
        Process Stop_Restart = new Process();
        Stop_Restart.StartInfo.UseShellExecute = false;
        Stop_Restart.StartInfo.RedirectStandardOutput = true;
        Stop_Restart.StartInfo.RedirectStandardError = true;
        Stop_Restart.StartInfo.RedirectStandardInput = true;
        Stop_Restart.StartInfo.FileName = Programmability.psexecpath;
        var cancel_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -a" };
        Stop_Restart.StartInfo.Arguments = String.Join(" ", cancel_args);
        {
            Stop_Restart.Start();
            string Output = Stop_Restart.StandardOutput.ReadToEnd();
            string Errormessage = Stop_Restart.StandardError.ReadToEnd();
            Stop_Restart.WaitForExit();
        }


    }




      }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统诊断;
使用System.IO;
命名空间媒体\u墙\u重新启动\u调度程序
{
公共部分类frm_mediawall_开始格式:表单
{
公共frm_mediawall_startform()
{
初始化组件();
input_time.Value=DateTime.Now;
}
静态类可编程性
{
/*安全凭据存储-这用于远程重新启动*/
公共静态只读字符串mediawallcomputername=@“\\computername”;
公共静态只读字符串adminusername=“computeraccount”;
公共静态只读字符串AdminDomainName=“domainname”;
公共静态只读字符串adminpassword=“computeraccountpassword”;
公共静态只读字符串addadminpassword=“-p”+adminpassword;
公共静态只读字符串adminuseraccount=AdminDomainName+@“\”+adminusername;
公共静态只读字符串addadminuseraccount=“-u”+AdminDomainName+@“\”+adminusername;
公共静态只读字符串Restartnow=mediawallcomputername+addadminuseraccount+addadminpassword+“shutdown-r-t30”;
公共静态只读字符串CancelShutdown=mediawallcomputername+addadminuseraccount+addadminpassword+“shutdown-a”;
公共静态只读字符串psexecpath=Path.GetFullPath(Path.Combine(AppContext.BaseDirectory,“Resources”,“PsExec.exe”);
/*结束安全凭据存储*/
}
私有无效Btn\u立即重新启动\u单击(对象发送方,事件参数e)
{
/*快速简单的立即重启命令*/
进程重新启动_now=新进程();
重新启动_now.StartInfo.UseShellExecute=false;
重新启动_now.StartInfo.RedirectStandardOutput=true;
重新启动_now.StartInfo.RedirectStandardError=true;
重新启动_now.StartInfo.RedirectStandardInput=true;
重新启动_now.StartInfo.FileName=Programmability.psexecpath;
var Restartnow_args=新列表{Programmability.mediawallcomputername,Programmability.addadminuseraccount,Programmability.addadminpassword,“shutdown-s-t30”};
Restart\u now.StartInfo.Arguments=string.Join(“,Restartnow\u args);
{
立即重新启动_.Start();
string Output=Restart_now.StandardOutput.ReadToEnd();
string Errormessage=Restart_now.StandardError.ReadToEnd();
立即重新启动_.WaitForExit();
}
}
公共无效Btn\u已计划\u重新启动\u单击(对象发送方,事件参数e)
{
/*获取当前时间*/
DateTime Timenow=DateTime.Now;
DateTime Restarttime=输入\ u time.Value;
/*获取计划时间并从上面的“现在”时间中减去*/
var ScheduledRestartTime=Math.天花((Restarttime-Timenow.TotalSeconds);
/*运行命令在指定时间重新启动媒体墙*/
进程定时_重新启动=新进程();
Timed_Restart.StartInfo.UseShellExecute=false;
Timed_Restart.StartInfo.RedirectStandardOutput=true;
Timed_Restart.StartInfo.RedirectStandardError=true;
Timed_Restart.StartInfo.RedirectStandardInput=true;
Timed_Restart.StartInfo.FileName=Programmability.psexecpath;
var TimedRestart_args=新列表{Programmability.mediawallcomputername,Programmability.addadminuseraccount,Programmability.addadminpassword,“shutdown-r”,$“-t{ScheduledRestartTime}”;
Timed_Restart.StartInfo.Arguments=string.Join(“,TimedRestart_args);
{
定时_Restart.Start();
string Output=Timed_Restart.StandardOutput.ReadToEnd();
string Errormessage=Timed_Restart.StandardError.ReadToEnd();
定时重新启动。WaitForExit();
}
}
私有无效Btn\u停止\u重新启动\u单击(对象发送方,事件参数e)
{
进程停止\重新启动=新进程();
Stop_Restart.StartInfo.UseShellExecute=false;
Stop\u Restart.StartInfo.RedirectStandardOutput=true;
Stop\u Restart.StartInfo.RedirectStandardError=true;
Stop\u Restart.StartInfo.RedirectStandardInput=true;
Stop_Restart.StartInfo.FileName=Programmability.psexecpath;
var cancel_args=新列表{Programmability.mediawallcomputername,Programmability.addadminuseraccount,Programmability.addadminpassword,“shutdown-a”};
Stop\u Restart.StartInfo.Arguments=String.Join(“,cancel\u args);
{
Stop_Restart.Start();
字符串输出=停止\重新启动.StandardOutput.ReadToEnd();
string Errormessage=Stop\u Restart.StandardError.ReadToEnd();
Stop_Restart.WaitForExit();
}
}
}
}
在尝试捕捉错误时,我添加了以下更改,但这并没有帮助我进一步了解。我将在这里添加一个用于显示的控件,但它已应用于每个按钮

private void Btn_stop_restart_Click(object sender, EventArgs e)
    {
        try
        {
            Process Stop_Restart = new Process();
            Stop_Restart.StartInfo.UseShellExecute = false;
            Stop_Restart.StartInfo.RedirectStandardOutput = true;
            Stop_Restart.StartInfo.RedirectStandardError = true;
            Stop_Restart.StartInfo.RedirectStandardInput = true;
            Stop_Restart.StartInfo.FileName = Programmability.psexecpath;
            var cancel_args = new List<string> { Programmability.mediawallcomputername, Programmability.addadminuseraccount, Programmability.addadminpassword, "shutdown -a" };
            Stop_Restart.StartInfo.Arguments = String.Join(" ", cancel_args);
            using (var stopreader = Stop_Restart.StandardOutput)
            {
                Stop_Restart.BeginOutputReadLine();
                Stop_Restart.Start();
                string Output = Stop_Restart.StandardOutput.ReadToEnd();
                string Errormessage = Stop_Restart.StandardError.ReadToEnd();
                Stop_Restart.WaitForExit();
                var StopConsoleOut = stopreader.ReadToEnd();
                MessageBox.Show(StopConsoleOut, "Success", MessageBoxButtons.OK);
            }
        }
        catch (Exception)
        {
            MessageBox.Show($"Running psexec failed as {Programmability.psexecpath}", "Error", MessageBoxButtons.OK);
            throw;
        }
    }
private void Btn\u停止\u重新启动\u单击(对象发送方,事件参数e)
{
尝试
{
Restart_now.Start();
string Output = Restart_now.StandardOutput.ReadToEnd();
string Errormessage = Restart_now.StandardError.ReadToEnd();
MessageBox.Show(Errormessage);
Restart_now.WaitForExit();