使用C#将命令发送到DOS [英] Send commands to DOS using C#

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

问题描述

大家好,

这是我在C#世界中的第一个项目/职位.

我想使用C#从DOS发送和读取命令.
例如.使用edit myfile.txt命令创建/删除文件

Hello All,

This is my first project/post in C# world.

I want to send and read the commands from DOS using C#.
For eg. creating/deleting a file using edit myfile.txt command

System.Diagnostics.Process proc = new System.Diagnostics.Process();
              proc.EnableRaisingEvents = false;
              string strCmdLine;
              strCmdLine = "edit kiran.txt";
              System.Diagnostics.Process.Start("CMD.exe", strCmdLine);



但是在上面的代码中,仅打开了命令提示符.我无法创建文件.

提前谢谢.
Kirangowle



but in above code only command prompt is getting opened.i cant able to create a file.

Thanks in advance.
Kirangowle

推荐答案

首先,请参阅我对该问题的评论:在它的范围内,没有"DOS"之类的东西. (那是您犯错的根本原因.)

现在,要解决您的问题,您只需要了解您不需要"CMD.EXE".这只不过是另一个与您要解决的问题无关的程序:启动应用程序.

因此,您所需要做的就是:
First, please see my comment to the question: in the scope of it, there is no such thing as "DOS". (And that was a root cause of your mistake.)

Now, to fix your problem you just need to understand that you don''t need "CMD.EXE". This is nothing but yet another program which is irrelevant to the problem you are trying to solve: starting an application.

So, all you need is this:
string app = "edit";
string strCmdLine = "kiran.txt";
System.Diagnostics.Process.Start(app, strCmdLine);



现在,我不知道什么是编辑",但这是文本编辑器吗?很可能您真的不想启动它.它将无法与您的应用程序协作.创建UI应用程序(WPF或System.Windows.Forms)并在应用程序中嵌入诸如TextBox之类的简单编辑控件会更加合理.

-SA



Now, I don''t know what is "edit", but is this is some text editor, chances are, you don''t really want to start it; it won''t be able to collaborate with your application. It would be more reasonable to create a UI application (WPF or System.Windows.Forms) and embed a simple edit control like TextBox in your application.

—SA


您可以在以下位置看到它: ^ ].希望这会有所帮助.
You can see it at: How to Execute a Command in C# ?[^]. Hope this helps.


这篇关于使用C#将命令发送到DOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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