Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用Process.Start()作为cmd和活动输出的参数执行应用程序_C#_Clickonce - Fatal编程技术网

C#使用Process.Start()作为cmd和活动输出的参数执行应用程序

C#使用Process.Start()作为cmd和活动输出的参数执行应用程序,c#,clickonce,C#,Clickonce,我正在使用Visual Studio ClickOne并尝试执行(.appref ms)应用程序 并使用重定向标准输出 我必须使用选项“首选32位”,因为我正在使用连接字符串为Provider=Microsoft.Jet.OLEDB.4.0的Access DB。 这是我的执行代码: var p = new Process(); p.StartInfo = new ProcessStartInfo(@"C:\Users\hed-b\Desktop\PulserTester.appr

我正在使用Visual Studio ClickOne并尝试执行
(.appref ms)应用程序
并使用
重定向标准输出

我必须使用选项
“首选32位”
,因为我正在使用连接字符串为
Provider=Microsoft.Jet.OLEDB.4.0的Access DB。

这是我的执行代码:

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(@"C:\Users\hed-b\Desktop\PulserTester.appref-ms")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false
    };
    p.Start();
    reportNumber = p.StandardOutput.ReadToEnd();
    p.WaitForExit();
这就是我犯的错误

System.ComponentModel.Win32Exception:'指定的可执行文件不可用 此操作系统平台的有效应用程序。'

编辑

看这里,我看到我可以通过cmd运行它

作为


但我如何才能以这种方式使用RedirectStandardOutput?

看起来您打算启动CMD并运行命令,但您的代码只是尝试将该命令作为应用程序运行

试试这样的

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(@"cmd.exe")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        Arguments = "/c C:\Users\hed-b\Desktop\PulserTester.appref-ms"
    };
    p.Start();
    reportNumber = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

使用阻塞
ReadToEnd
将在代码运行时暂停线程,并使捕获错误输出变得更加困难-查看此答案,以演示一种捕获标准数据和标准错误的非阻塞解决方案:

看起来您打算启动CMD并运行命令,但您的代码只是尝试将该命令作为应用程序运行

试试这样的

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(@"cmd.exe")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        Arguments = "/c C:\Users\hed-b\Desktop\PulserTester.appref-ms"
    };
    p.Start();
    reportNumber = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

使用blocking
ReadToEnd
将在代码运行时暂停线程,并使捕获错误输出变得更加困难-请看下面的答案,以演示一种捕获标准数据和标准错误的非阻塞解决方案:

我想您可以试试这个,'ProcessStartInfo info=newprocessstartinfo(“cmd.exe”);info.Arguments=@”/c:\Users\hed-b\Desktop\pulsertest.appref ms”;设置重定向标准输出所需的信息,然后设置“Process Process=Process.Start(info)你不能,重定向I/O需要使用ProcessStartInfo.UseShellExecute=false;这会阻止启动appref ms文件。只要你对硬编码路径名感到恐惧,你就可以全力以赴,硬编码C:\Windows\System32\rundll32.exe C:\Windows\System32\dfshim.dll,ShOpenVerbShortcut,“yourpath.appref-ms”“。这可能不起作用,rundll32.exe不是控制台模式的应用程序,因此没有可重定向的内容。ClickOnce不是您最喜欢的部署技术,除非您使用它来部署这两个可执行文件;info.Arguments=@“/c:\Users\hed-b\Desktop\pulsertest.appref-ms”;使用RedirectStandardOutput设置所需的信息,然后设置“Process Process=Process.Start”(信息)如果不能,重定向I/O需要使用ProcessStartInfo.UseShellExecute=false;这会阻止启动appref ms文件。只要你致力于硬编码路径名的恐惧,你可能会全力以赴,硬编码C:\Windows\System32\rundll32.exe C:\Windows\System32\dfshim.dll,ShOpenVerbShortcut,“yourpath.appref ms”。这不太可能奏效,rundll32.exe不是控制台模式的应用程序,因此没有任何重定向。ClickOnce不是您最喜欢的部署技术,除非您使用它来部署这两个可执行文件。谢谢您的回答。。我尝试了这个问题,它是这个(.appref ms)文件,它只是我的程序的运行程序(VisualStudio的一部分),我真正的程序不等待WaitForExit();谢谢你的回答。。我尝试了这个问题,它是这个(.appref ms)文件,它只是我的程序的运行程序(VisualStudio的一部分),我真正的程序不等待WaitForExit();