通过WPF按钮在服务器上运行文件

时间:2019-04-09 19:11:47

标签: c# wpf psexec system.diagnostics

使用Process.Start()PsExec.exe在serverpc上启动视频的某些应用程序代码在从控制台应用程序运行时运行正常,但在WPF应用程序中按按钮运行时运行不正常。今天让我发疯了。

所以:

我正在PC上运行一个小型WPF应用程序,一旦按下按钮,它将向服务器PC发送命令以运行视频文件。我正在使用PsExec.exe在服务器上以交互方式运行该过程(未通过WMI管理) 这是我正在使用的代码:


Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\Windows\System32\PsExec.exe";
p.StartInfo.Arguments = @"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4";
            p.StartInfo.CreateNoWindow = true;
            p.Start();

否,当从普通控制台应用程序运行时,此完全相同的代码可以在服务器PC(192.168.1.3)上打开视频文件Photoslideshow.mp4。但是,当我尝试在WPF应用程序中按下按钮后运行它时,p.Start()给我一个“系统找不到指定的文件”错误。这是WPF代码段(与上面的代码相同):

private void Video1_btn_Click(object sender, RoutedEventArgs e)
{
    try
    {
        Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.FileName = @"C:\Windows\System32\PsExec.exe";
        p.StartInfo.Arguments = @"\\192.168.1.3 -u Administrator -p hagarmikejessav -i cmd.exe /c START E:\Media\FerroniConcettaAapp\Videos\Photoslideshow.mp4  //fullscreen";
        p.StartInfo.CreateNoWindow = true;
        p.Start();
    }
}

当我尝试在PC本地文件上使用p.Start时,按预期打开了该文件。只是服务器没有“看到” FileName。就像我最初说的那样,相同的代码仅在单击按钮后访问p.Start时失败。

我在做什么错?请有人告诉我,这是PC机前数小时的结果,这只是我看不到的愚蠢错误!

编辑:

经过更多的试验之后,我意识到与以下行相关的错误“系统找不到指定的文件”: p.StartInfo.FileName = @“ C:\ Windows \ System32 \ PsExec.exe”;

enter image description here

将此行更改为:

p.StartInfo.FileName = @“ C:\ Windows \ System32 \ Notepad.exe”;

并删除下一行,记事本在我的本地PC上打开。但是,当我将两行更改回类似:

p.StartInfo.FileName = @“ Notepad.exe”; p.StartInfo.Arguments = @“ \ 192.168.1.3 -u管理员-p pass-i cmd.exe / c START C:\ realtek.txt”; ...

记事本在本地PC上打开,但出现错误“找不到网络路径”。 (与我运行“非按钮代码”时类似的错误。)

因此,我知道问题与WPF / Button应用程序有关。但是我不知道问题出在哪里!

非常感谢, 马里奥

1 个答案:

答案 0 :(得分:0)

我设法弄清楚了。

由于某种原因,系统无法从该路径中找到PSExec(即使该文件夹中存在PSExec)。 p.StartInfo.FileName = @“ C:\ Windows \ System32 \ PsExec.exe”;

将文件复制到另一个目录并使用完整路径后,它终于可以工作了。