C
C#15mo ago
Seventy

❔ Getting $StandardIn has not been redirected.$ Error...

public class CmdExecutor
{
private Process process;

public CmdExecutor()
{
InitializeProcess();
}

private void InitializeProcess()
{
process = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};

process.StartInfo = startInfo;


process.OutputDataReceived += Process_OutputDataReceived;

process.Start();
process.BeginOutputReadLine();
}

public string ExecuteCommand(string command)
{
try
{
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
return "";
}
catch (Exception ex)
{
Console.WriteLine("Error executing command: " + ex.Message);
return "";
}
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

public void CloseProcess()
{
if(process != null)
{

process.Close();
process.Dispose();
}
}
}
public class CmdExecutor
{
private Process process;

public CmdExecutor()
{
InitializeProcess();
}

private void InitializeProcess()
{
process = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};

process.StartInfo = startInfo;


process.OutputDataReceived += Process_OutputDataReceived;

process.Start();
process.BeginOutputReadLine();
}

public string ExecuteCommand(string command)
{
try
{
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
return "";
}
catch (Exception ex)
{
Console.WriteLine("Error executing command: " + ex.Message);
return "";
}
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

public void CloseProcess()
{
if(process != null)
{

process.Close();
process.Dispose();
}
}
}
When i run the execute command method i get StandardIn has not been redirected. as an error. Does anyone know what's up with that?
7 Replies
boiled goose
boiled goose15mo ago
apart from the fact that having process.Start() in the constructor is horrible and that there is no process.WaitForExit() and also why ExecuteCommand should always return "", not even String.Empty that's weird, i don't see why it should give errors
Seventy
Seventy15mo ago
Thank you for taking some time out of your day to look at my post. Firstly, what do you mean by horrible process.Start() constructor and how would i make it less horrible? Secondly ? have you tried to run the code? if not, can I ask you to do so? This is driving me insane dead1dead2 Thirdly trol what do you mean with ExecuteCommand should always return "", is it not? forget "Thirdly" my brain just kickstarted.
boiled goose
boiled goose15mo ago
i said why ExecuteCommand should return "", evidently it's not used
Andrea Tate
Andrea Tate15mo ago
True
Chiyoko_S
Chiyoko_S15mo ago
Chiyoko_S
Chiyoko_S15mo ago
Seems to work fine? fwiw I did
var exec = new CmdExecutor();
exec.ExecuteCommand("a");
var exec = new CmdExecutor();
exec.ExecuteCommand("a");
I'd assume ExecuteCommand is supposed to return whatever is written to stdout, I don't see any problem with that, probably just not implemented yet though I'd say.... booting up a cmd session to do run commands kinda seem pointless? why not just invoke the programs directly? is this just a toy thing or do you intend to actually use this for something else
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts