Question

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...?

Était-ce utile?

La solution

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

Autres conseils

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.

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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top