ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Why is Process.StandardInput.WriteLine not supplying input to my process?

This question was asked on StackOverflow but none of the answers are working. The StreamWriter Flushes the input and carpos private field is set to zero but the BaseStream carpos is still stuck at the length of the buffer. None of the input was passed to the process.

Any ideas of why or how to work around this? I used dotnet framework4.7.1 and also tried it on4.5.2

user-image
تم إضافة السؤال من قبل مستخدم محذوف‎
تاريخ النشر: 2018/03/13

I got it to work :)

 

Process _process;

StreamWriter _inputWriter;

 

void Main()

{

_process = new Process

{

EnableRaisingEvents = true,

StartInfo = new ProcessStartInfo

{

FileName = "cmd.exe",

Arguments = string.Empty,

UseShellExecute = false,

CreateNoWindow = true,

WindowStyle = ProcessWindowStyle.Hidden,

WorkingDirectory = Directory.GetCurrentDirectory(),

StandardOutputEncoding = Encoding.UTF8,

StandardErrorEncoding = Encoding.UTF8,

RedirectStandardInput = true,

RedirectStandardOutput = true,

RedirectStandardError = true

}

};

 

_process.OutputDataReceived += (s, e) => // instead of using a background worker

{

if (e.Data == null) return;

Console.WriteLine(e.Data);

};

 

Console.WriteLine("Starting...");

if (!_process.Start()) return;

_process.BeginOutputReadLine(); // <- using BeginOutputReadLine

_inputWriter = _process.StandardInput;

_inputWriter.AutoFlush = true;

_inputWriter.WriteLine(); // <- my little trick here

 

string input = Util.ReadLine<string> ("Enter command:"); // using LINQPad, replace it with Console.ReadLine();

if (!string.IsNullOrEmpty(input)) _inputWriter.WriteLine(input);

_process.WaitForExit(5000);

_process.Kill();

Console.WriteLine("Done");

}

 

void OnOutput(string data)

{

Console.WriteLine(data);

}

 

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟