问题在cmd中执行命令使用C# [英] issue in executing commands in cmd using C#

查看:296
本文介绍了问题在cmd中执行命令使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#应用程序的cmd中执行两个命令。

I am doing two commands in cmd from C# application.


  1. 重命名文件

  1. renaming a file

string commandToExecute;
commandToExecute = @"/c ren E:\filename filename.rar";
Process.Start("cmd.exe", commandToExecute);


  • 解压缩档案

  • unrar-ing a file

    commandToExecute = @"/c unrar e E:\filename.rar";
    Process.Start("cmd.exe", commandToExecute);
    


  • 代码的第一部分工作,第二部分没有,虽然它是工作时,我在 cmd 手动写命令。我注意到,当在C#中执行它再次运行应用程序本身(像递归)。我不知道为什么。

    The first part of the code works, but the second part doesn't, although it is working when I write the command in cmd manually. And I noticed that when executing in C# it runs the application itself again (like recursion). I don't know why.

    注意:我使用64位Windows 7。

    Note: I am using 64-bit windows 7.

    推荐答案

    记住Process.Start启动另一个进程。如果命令需要串行运行,则需要从Process.Start中获取一个Process对象并调用WaitForExit方法。

    Remember that Process.Start launches another process. If the commands need to run serially, you need to obtain a Process object from Process.Start and call the WaitForExit method.

    这篇关于问题在cmd中执行命令使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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