c#执行DOS命令并存储结果 [英] c# execute DOS command and store the results

查看:139
本文介绍了c#执行DOS命令并存储结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命令提示符下,我可以输入ipconfig,将显示一个互联网信息列表。我想在C#控制台程序中执行此操作。基本上,我想运行C#程序,我想读取和存储c:\ ipconfig中给出的所有内容。



ipconfig是一个我可以设置为任何内容的字符串和c:\是文件夹目录。



是否可以在C#中执行此操作?感谢任何信息。



FYI:这个例子可能很傻但我正在研究一个与此非常相似的问题,显然这是唯一的办法它。我没有任何API。





编辑:



由于每个人都对这些问题感到困惑,这里有更多详细信息:



我在命令提示符下执行的操作直接导航到我安装应用程序的特定位置发布一个commnad和一个信息列表将会出现。我想捕获这些信息。而且,我想在C#中完成所有操作,因为我想将该信息处理到我所做的其他应用程序中。

解决方案

此应用程序不应该在C中: 。你知道系统甚至不需要驱动器C吗?!

这不是DOS命令。在现代操作系统中,没有这样的东西。



只需使用 System.Diagnostics.Process.Start 重定向 StandardOutput

http://msdn.microsoft.com/en-us/library/0w4h05yb(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us /library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx [ ^ ](代码示例可在此处找到),

< a href =http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx> http://msdn.microsoft.com/en-us/ librar y / system.diagnostics.processstartinfo(v = vs.110).aspx [ ^ ]。



-SA


为什么要通过向'命令提示符'发送一些命令来折磨自己?

使用.net类,对象是不是更好?



请参阅:

.NET Framework中的网络编程 [ ^ ]

NetworkInterface Class [ ^ ]



常用的替代方案是 WMI [ ^ ],提供获取有关连接到计算机的任何设备的信息的功能。

更多: WMI课程 [ ^ ]


我的 ProcessCommunicator [ ^ ]文章可能会让您了解如何做到这一点以及一些陷阱。 />






我还想指出,处理输出的控制台应用程序可能不需要实际上执行命令。您可以编写一个处理器应用程序,从管道读取其输入:



someutility |处理器

In a command prompt I can type "ipconfig" and a list of internet information will appears. I want to do this in a C# console program. Basically, I want to run the C# program and I want to read and store everything that is given in "c:\ipconfig".

"ipconfig" is a String in which that I can set to be anything and "c:\" is the folder directory.

Is it possible to do that in C#? Any information is appreciated.

FYI: This example may be silly but I am working on a problem that is very similar to this and apparently it is the only way to do it. I do not have any API for it.


Edit:

Since everyone is confused of the questions, here is it in more details:

All I do in command prompt is navigate to a specific directly where I installed my application and issuing a commnad and a list of info will show up. I want to capture this info. And, I want to do this all in C# because I then want to process that info into other applications that I made.

解决方案

This application should not be in C:. Do you know that a system does not even have to have drive C?!
And this is not a "DOS command". In modern OS, there is no such thing.

Simply use System.Diagnostics.Process.Start with redirection of StandardOutput:
http://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx[^] (code sample can be found here),
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx[^].

—SA


Why to torture yourself by sending some command to the 'command prompt'?
Isn't better to use .net classes, objects?

Please, see:
Network Programming in the .NET Framework[^]
NetworkInterface Class[^]

Quite often used alternative is WMI[^], which provides functionality to get information about any devices connected to the computer.
More: WMI Classes[^]


My ProcessCommunicator[^] article may give you an idea of how to do that and some of the pitfalls.



I'd also like to point out that maybe the console app that processes the output needn't actually execute the command. You can write a processor app that reads its input from a pipe:

someutility | processor


这篇关于c#执行DOS命令并存储结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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