如何从您自己的C#代码运行另一个应用程序? [英] How to run another application from your own C# code?

查看:168
本文介绍了如何从您自己的C#代码运行另一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个名为Analyze.exe的程序.通常是从命令行运行的,如下所示:

c:\ analyze.exe"C:\ some path \ filename.dat""C:\ some path \ filename.out"



我想从某些C#代码运行该程序,但是失败了.似乎失败了,因为传递的文件名中有空格.如果有人知道我在做什么错或有其他想法要完成此操作,请指出!!

Hello everyone,

I have a program called Analyze.exe. It is normally ran from the command line like this:

c:\analyze.exe "C:\some path\filename.dat" "C:\some path\filename.out"



I want to run this program from some C# code, but it is failing. It appears to be failing because there are spaces in the filename being passed in. If anyone knows what I am doing wrong or has another idea how to accomplish this please point it out!

public void runProcess()
{
   
   string inputPath = "\"C:\\some path\\filename.dat\"";
   string outPutPath = "\"C:\\some path\\filename.out\"";

   System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
   pProcess.StartInfo.FileName = "c:\\analyze.exe"
   pProcess.StartInfo.Arguments = inputPath + " " + outputPath;
   pProcess.Start();
   pProcess.WaitForExit();
   pProcess.Close();
}



在方法体内执行第5行之后-
如果要轮询pProcess.StartInfo.Arguments属性值,则会看到期望的结果. (一个在字符串的开头和结尾带有引号的字符串.)


在执行上述代码时,自变量中的引号被省略了,从而导致下游问题!


为了测试这一点,我用下面列出的程序替换了实际的Analyze.exe.



After the execution of the 5th line inside the method body --
If you were to poll the pProcess.StartInfo.Arguments property value, you see what you would expect. ( a string with Quote marks at the begining and ending of the string. )


Somewhere in the execution of the above code the Quote marks are being omitted from the Arguments causing problems down stream!


To test this I replaced the actual Analyze.exe with this program listed below.

class Program
{
   static void Main( string[] args )
   {
      for ( int i = 0; i < args.Length; i++ )
      {
         Console.WriteLine("args[i] = " + args[i] );
      }

      Console.ReadLine();
   }



任何帮助将不胜感激,

Mike



Any help will be deeply appreciated,

Mike

推荐答案

好问题Mike.

这是您可以执行的操作:

Good question Mike.

This is how you can do this:

string inputPath = "\"\\\"C:\\some path\\filename.dat\\\"\"";
string outPutPath = "\"\\\"C:\\some path\\filename.out\\\"\"";


也许,只是注释:

1)您是否知道可以将控制台应用程序的输出重定向到任何流(不仅是文件,还可以是某些旨在读取和显示UI或类似结果的流)?请参见以下示例: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx [
Maybe, just side notes:

1) Do you know that you can re-direct output of the console application to any stream (not only the file, but, say, some stream designed to read and show results in UI or something like that)? See this sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^]. You can redirect both stdout and stderr, see System.Diagnostics.Process.StandardOutput, System.Diagnostics.Process.StandardInput, see also System.Diagnostics.Process.StandardInput.

2) If you use Process.WaitForExit() (you may or may not need it), you should understand this is blocking call, so you need to do it all in a separate thread. This is easy enough.

—SA


这篇关于如何从您自己的C#代码运行另一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆