5

If I compile this c# code to an EXE file and run in a Windows command shell it runs fine: outputting the prompt, waiting on the same line for some user input followed by enter, echoing that input. Running in a PowerShell v3 shell it also runs fine. If, however, I run this same EXE file in PowerShell ISE V3, it never emits output from Write and it hangs on the ReadLine. (As an aside, it will emit output from Write if it is later followed by a WriteLine.)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.Write("invisible prompt: ");
            var s = System.Console.ReadLine();
            System.Console.WriteLine("echo " + s);
        }
    }
}

Is this an ISE bug or is there some property to adjust to make it work...?

Michael Sorens
  • 35,361
  • 26
  • 116
  • 172
  • Not sure if it is relevant but in Powershell it sometimes called *Keyboard Polling* which can implement certain issues. So maybe when `Console.ReadLine()` is called due to Powershell using that polling it maybe causing a conflict. – Greg Apr 19 '13 at 21:15

3 Answers3

4

The ISE has no support for the console class.

  • No support for the [Console] class, try [console]::BackgroundColor = 'white'.
    • In general, scripts should use the host API's (write-host, instead of the [Console] class, so that they work in both the console, ISE, Remoting and other shells.

Reference: http://blogs.msdn.com/b/powershell/archive/2009/04/17/differences-between-the-ise-and-powershell-console.aspx

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Andy: You have made me realize that the question I should have asked depends on the answer: if there was a workaround/setting/etc. then this question would be good as is, but since you point out there is _not_ then the better question should have been something like: _is there some way to read inputs in C# from ISE without using the console?_ But I did not know that a priori. Anyway, you answered it as asked so you get the checkmark--thanks! – Michael Sorens Apr 21 '13 at 22:55
4

As a workaround, in powershell ise, instead of calling .\ConsoleApplication1.exe call start .\ConsoleApplication1.exe This will start the application in a new command prompt, that will accept input from stdin.

user75810
  • 829
  • 7
  • 14
0

Powershell read-host will read from the console. You could execute the read-host from within C#. Executing PS commands from C# is documented on MSDN in the sections about Powershell hosting. The following demonstrates the methods calls with a Powershell script - should be easy to convert to C#. Note that $host must be passed into the C# program. Therefore you'd have to load the C# program as an assembly within ISE and invoke Main() like: [myclass]::Main($host) (versus invoking the EXE as a standalone program)

[powershell]::create().AddScript('param($ho) $ho.ui.readline()').AddArgument($host).Invoke()
Χpẘ
  • 3,403
  • 1
  • 13
  • 22