gpt4 book ai didi

c# - 如何在 C# 中将 "<"(输入重定向)与 ProcessStartInfo 一起使用?

转载 作者:行者123 更新时间:2023-11-30 19:30:28 29 4
gpt4 key购买 nike

我有以下内容:

C:\temp\dowork.exe < input.txt
processing.......
complete
C:\

我试试这个:

processArguments = " < input.txt";
pathToExe = "C:\\temp\dowork.exe";
startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(pathToExe),
Arguments = processArguments
};

try
{
using (_proc = Process.Start(startInfo))
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}

调用 Start() 后我的 dowork.exe 崩溃了。

有什么建议吗?

发布问题更新。

谢谢大家的意见。我使用 amit_g 的答案解决了这个问题。非常感谢 Phil 展示了可能是最好的方法(虽然我没有测试它,但我明白为什么它更好)。以下是我的完整解决方案。您可以根据自己的问题随意复制和修改。

1)创建一个控制台应用项目,添加这个类

internal class DoWork
{
private static void Main(string[] args)
{
var fs = new FileStream("C:\\temp\\output.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None);

var toOutput = "Any items listed below this were passed in as args." + Environment.NewLine;
foreach (var s in args)
toOutput += s + Environment.NewLine;

Console.WriteLine("I do work. Please type any letter then the enter key.");
var letter = Console.ReadLine();
Console.WriteLine("Thank you.");
Thread.Sleep(500);

toOutput += "Anything below this line should be a single letter." + Environment.NewLine;
toOutput += letter + Environment.NewLine;

var sw = new StreamWriter(fs);
sw.Write(toOutput);

sw.Close();
fs.Close();
}
}

2) 创建1个文件:C:\temp\input.txt

3) 编辑input.txt,输入一个字母'w'并保存(没错,文件只包含一个字母)。

4) 创建一个新的类库项目。添加对 nunit 的引用(我使用的是 2.2 版)。

5) 创建一个 testfixture 类,它应该如下所示。注意:此测试装置正在处理外部资源,因此您不能运行整个装置,而是一次运行每个测试。您可以通过确保关闭所有文件流来解决此问题,但我不想写这个,您可以随意扩展它。

using System.Diagnostics;
using System.IO;
using NUnit.Framework;

namespace Sandbox.ConsoleApplication
{
[TestFixture]
public class DoWorkTestFixture
{
// NOTE: following url explains how ms-dos performs redirection from the command line:
// http://www.febooti.com/products/command-line-email/batch-files/ms-dos-command-redirection.html

private string _workFolder = "C:\\Temp\\";
private string _inputFile = "input.txt";
private string _outputFile = "output.txt";
private string _exe = "dowork.exe";

[TearDown]
public void TearDown()
{
File.Delete(_workFolder + _outputFile);
}

[Test]
public void DoWorkWithoutRedirection()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder
};

var process = Process.Start(startInfo);
process.WaitForExit();

Assert.IsTrue(File.Exists(_workFolder + _outputFile));
}

[Test]
public void DoWorkWithoutRedirectionWithArgument()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
Arguments = _inputFile
};

var process = Process.Start(startInfo);
process.WaitForExit();

var outputStrings = File.ReadAllLines(_workFolder + _outputFile);

Assert.IsTrue(File.Exists(_workFolder + _outputFile));
Assert.AreEqual(_inputFile, outputStrings[1]);
}

[Test]
public void DoWorkWithRedirection()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
RedirectStandardInput = true
};

var myProcess = Process.Start(startInfo);
var myStreamWriter = myProcess.StandardInput;
var inputText = File.ReadAllText(_workFolder + _inputFile);

myStreamWriter.Write(inputText);

// this is usually needed, not for this easy test though:
// myProcess.WaitForExit();

var outputStrings = File.ReadAllLines(_workFolder + _outputFile);

Assert.IsTrue(File.Exists(_workFolder + _outputFile));
// input.txt contains a single letter: 'w', it will appear on line 3 of output.txt
if(outputStrings.Length >= 3) Assert.AreEqual("w", outputStrings[2]);
}

[Test]
public void DoWorkWithRedirectionAndArgument()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
RedirectStandardInput = true
};

var myProcess = Process.Start(startInfo);
var myStreamWriter = myProcess.StandardInput;
var inputText = File.ReadAllText(_workFolder + _inputFile);

myStreamWriter.Write(inputText);
myStreamWriter.Close();

// this is usually needed, not for this easy test though:
// myProcess.WaitForExit();

var outputStrings = File.ReadAllLines(_workFolder + _outputFile);

Assert.IsTrue(File.Exists(_workFolder + _outputFile));
// input.txt contains a single letter: 'w', it will appear on line 3 of output.txt
Assert.IsTrue(outputStrings.Length >= 3);
Assert.AreEqual("w", outputStrings[2]);
}


}

最佳答案

您必须使用 STDIN 重定向。像这样...

inputFilePath = "C:\\temp\input.txt";
pathToExe = "C:\\temp\dowork.exe";

startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(pathToExe),
RedirectStandardInput = true
};

try
{
using (_proc = Process.Start(startInfo))
{
StreamWriter myStreamWriter = myProcess.StandardInput;

// Use only if the file is very small. Use stream copy (see Phil's comment).
String inputText = File.ReadAllText(inputFilePath);

myStreamWriter.Write(inputText);
}

_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}

关于c# - 如何在 C# 中将 "<"(输入重定向)与 ProcessStartInfo 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9024476/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com