-1

i have following problem:

I am making a Windows Forms application and have to use other .cs Files which contain Console.Read() or Console.ReadLine(). The files should not be changed. I would like now to override the functions in order to use a button or a Textbox + Button instead of the Console.Read().

Since i should not change the .cs files, this seems not to be too trivial. Any ideas or leads?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Flomax
  • 1
  • Chck this, but not 100% sure if the method can work with static classes: http://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method – Gusman May 05 '17 at 13:30
  • @Gusman Sadly it is not quite what i want, or maybe luckily? What i need is more something that would replace the Console.Read() with a "wait until Return was clicked" sniplet. – Flomax May 05 '17 at 13:52
  • Well, that example shows how to replace a function of an existing class, so you can replace the Read() function with your own, but again, not sure if it will work for static classes like Console. – Gusman May 05 '17 at 13:54

1 Answers1

0

You can access the Console as a stream using:

var stdIn = Console.OpenStandardInput();

Then when you click the button, you can call

stdIn.Write(Encoding.ASCII.ToBytes(txtMyBuffer.Text), 0, txtMyBuffer.Text.Length);

From reference: https://msdn.microsoft.com/en-us/library/system.console.openstandardinput(v=vs.110).aspx

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Jon
  • 3,230
  • 1
  • 16
  • 28
  • First of all, thank you for your fast answere. The thing is, the Console.Read() is not overriden or redirected by snipplet. Somewhere in the .cs files a Console.Read() will pop up. Now i have to "satisfy" that Console.Read() with a Button click from a string (which can be from a textbox, but that is a lot more trivial!) – Flomax May 05 '17 at 13:43
  • A console window is popping up? – Jon May 05 '17 at 13:44
  • No, the only thing is the Form itself – Flomax May 05 '17 at 13:48
  • Then when you write the stdIn stream, it should be read in by your code in Console.Read(). It's the same stream. – Jon May 05 '17 at 13:48
  • But before i get to the button, i get an exception: ("Cannot read keys when either application does not have a console or when console input has been redirected from a file.") – Flomax May 05 '17 at 13:55
  • Then I'm not sure, you may need to launch a console from your Forms app using PInvoke http://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application – Jon May 05 '17 at 14:17