在C#中的CMD窗口中生成旧版应用 [英] Spawn legacy app in CMD window in C#

查看:87
本文介绍了在C#中的CMD窗口中生成旧版应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行使用Process类从cmd窗口运行的旧版应用程序.

I need to run a legacy app that is run from a cmd window using the Process class.

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = "/C \"C:\\UTDSys\\UTD2TCM.exe –r " + Parameters.Candidate + "\"";
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false;  

try 
{ // Start the process with the info we specified.     
  // Call WaitForExit and then the using statement will close.     
  using (Process exeProcess = Process.Start(startInfo))     
  {         
    exeProcess.WaitForExit();     
  }  
} 
catch (Exception e) 
{     
  string sMsg = "Error copying the files to " + Parameters.FullPath + ".";
  HandleErrorMsg(e, sMsg);
  return; 
}


进程My2Com.exe应该在后台运行,但是,我始终收到一条消息,即从带有不同标志的cmd行运行时使用的文件丢失了.如果我按照cmd窗口C:\ MySys \ My2Com.exe –r FullyQualPath中的指示运行命令,则它将按预期运行.我尝试了几种不同的方法来建立Process类,但没有成功.

任何建议,将不胜感激.

谢谢

Tom R.


The process My2Com.exe should run in the background, however, I consistantly get the message that a file, used when run from the cmd line with different flags, is missing. If I run the command as indicated in a cmd window, C:\MySys\My2Com.exe –r FullyQualPath, it works as expected. I have tried several different ways to set up the Process class without success.

Any suggestions would be appreciated.

Thank you,

Tom R.

推荐答案

您不需要"CMD"来启动您的应用程序. "CMD"是另一个应用程序,仅此而已,它不会为您的功能添加任何内容.只需使用静态方法System.Diagnostics.Process.Start:
http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx [ ^ ].

您的情况:
You don''t need "CMD" to start your application. "CMD" is another application, nothing more, it does not add anything to your functionality. Simply use a static method System.Diagnostics.Process.Start:
http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx[^].

In your case:
System.Diagnostics.Process.Start("C:\\UTDSys\\UTD2TCM.exe", string.Format("-r {0}", Parameters.Candidate);



现在,请理解,在任何情况下,除非您要开发仅在开发计算机上都能工作的应用程序,否则诸如"C:\ UTDSys \ UTD2TCM.exe"之类的硬编码路径名就不会有用.此文件可能不会放置在此目录中,该目录可能不存在,甚至连卷"C:"的存在也不是必须的(在我的一台计算机上,没有这样的驱动器).文件名总是根据用户输入,一些配置文件,程序集位置,特殊目录"(与用户的帐户相关,每个用户或所有用户的帐户)等来计算的.

—SA



Now, please understand that there is no cases where the hard-coded path names like "C:\UTDSys\UTD2TCM.exe" can be useful, unless you want to develop an application which can work only on your development computer. This file may not be placed in this directory, the directory might not exist, and even the presence of the volume "C:" is not a must (on one of my computers, there is no such drive). The file name is always calculated based on user input, some configuration files, assembly location, "special directories" (related to users'' accounts, those per user or for all users), etc.

—SA


此修复程序涉及如何构造参数字符串:
The fix involves how the arguments string is constructed:
startInfo.FileName = "C:\\UTDSys\\UTD2TCM.exe";
startInfo.Arguments = "-r " + Parameters.FilePath.ToString();




or

startInfo.FileName = "C:\\UTDSys\\UTD2TCM.exe";
startInfo.Arguments = string.Format("-r {0}", Parameters.FilePath);



感谢大家的帮助.



Thanks to everyone for your help.


这篇关于在C#中的CMD窗口中生成旧版应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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