带参数的CMD命令 [英] CMD command with arguments
问题描述
我正在尝试运行一个看起来像这样的命令(来自C#)
c:\ MyCommand/LOAD> c:\ temp \ mycommand.txt
这是我尝试过的操作,但未创建文件.
流程流程= new Process();
Process.StartInfo.Arguments => c:\\ mycommand.txt";
Process.Start("MyCommand","/LOAD");
不会将输出通过管道传输到文件
//还尝试了以下
//Process.Start("MyCommand/LOAD",">c:\temp\mycommand.txt);
//Process.Start("MyCommand,"/LOAD> c:\ temp \ mycommand.txt);
I am trying to run a command (from C# ) that looks like this
c:\MyCommand /LOAD >c:\temp\mycommand.txt
This is what I tried but the file is not created.
Process Process = new Process();
Process.StartInfo.Arguments = " >c:\\mycommand.txt";
Process.Start("MyCommand","/LOAD");
does not pipe the output to a file
//also tried the following
//Process.Start("MyCommand /LOAD",">c:\temp\mycommand.txt");
//Process.Start("MyCommand", " /LOAD >c:\temp\mycommand.txt");
Any help is greatly appreciated
推荐答案
重定向可执行文件输出的另一种方法如下所示:
One another method to redirect the output of an executable is shown below:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"systeminfo.exe"; // Specify exe name.
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
StreamWriter sw = new StreamWriter(@"C:\textinfo.txt");
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
sw.Write(result);
sw.Close();
}
}
}
}
}
如果不在CMD中启动重定向,则该重定向将不起作用.
因此,您的命令行如下所示:
Redirection doesn''t work if you don''t launch it inside a CMD.
So, your command line would look like this:
CMD.exe /C "<fullpathto>\MyCommand.exe /LOAD > C:\Temp\MyCommand.txt"</fullpathto>
请记住,不允许普通用户在Windows 7中的C:\ Temp中创建文件.请改用TEMP环境变量返回的路径.它将最终出现在用户个人资料的Temp文件夹中.
Keep in mind that normal users are not allowed to create file in C:\Temp in Windows 7. Use the path returned by the TEMP environment variable instead. It will end up in the Temp folder in the users profile.
Process Process = new Process();
Process.StartInfo.Arguments = " >c:\\mycommand.txt";
Process.Start("MyCommand.exe","/LOAD >c:\temp\mycommand.txt");
试试看.
您是否尝试过先在命令提示符下执行命令.如果可以的话,这有效吗?在我们开始诊断代码是否有问题之前.
try that.
Have you tried executing the command in the command prompt first. If so does it work? Before we start to diagnose if something is wrong with the code.
这篇关于带参数的CMD命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!