C#一样执行命令提示符下命令行? [英] c# execute command line like command prompt?

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

问题描述

我想编写C#代码来执行一些命令提示行,结果存储到一个全局变量在C#中被用于在C#代码的其他部分进行进一步处理。

I want to write a c# code to execute some command prompt lines and store the result into a global variable in c# to be used for further processing in other part of the code in c#.

我买了设备和我安装驱动程序。对我来说,只有这样,才能获得其数据使用在命令提示符下的命令行。没有可用的API。但是,我想在C#中这样做,这就是为什么我有麻烦了。

I bought a device and I installed its driver. The only way for me to obtain its data is using command line in command prompt. There is no API available. But, I want to do in c# so that is why I am having trouble.

我有一些代码,它不会出于某种原因的作品。请注意,我用的参数输入=日期仅供说明用途。我的工作是不使用日期,但一些参数来获取数据。

I have some code and it doesn't works for some reason. Note, I used argument input = "date" is only for illustration purpose. My work is not using "date" but some arguments to obtain data.

static void Main(string[] args)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    // the cmd program
    startInfo.FileName = "cmd.exe";
    // set my arguments. date is just a dummy example. the real work isn't use date.
    startInfo.Arguments = "date";
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.Start();
    // capture what is generated in command prompt
    var output = process.StandardOutput.ReadToEnd();
    // write output to console
    Console.WriteLine(output);
    process.WaitForExit();

    Console.Read();
}



任何帮助表示赞赏。

Any help is appreciated.

推荐答案

您需要使用/ C日期以获得cmd以上市日期这样的。

you'll need to use /c date to get cmd to launch date like that.

static void Main(string[] args)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    // the cmd program
    startInfo.FileName = "cmd.exe";
    // set my arguments. date is just a dummy example. the real work isn't use date.
    startInfo.Arguments = "/c date";
    startInfo.RedirectStandardOutput = true;
    startInfo.UseShellExecute = false;
    process.StartInfo = startInfo;
    process.Start();
    // capture what is generated in command prompt
    var output = process.StandardOutput.ReadToEnd();
    // write output to console
    Console.WriteLine(output);
    process.WaitForExit();

    Console.Read();
}



标志/ c和/ k为使用CMD.EXE当你的朋友启动其他程序。 / c用于执行一个程序,然后退出CMD。 / K用于执行则程序离开CMD运行。

the flags /c and /k are your friends when using cmd.exe to launch other programs. /c is used to execute a program then exit CMD. /k is used to execute a program then leave CMD running.

这篇关于C#一样执行命令提示符下命令行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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