简体   繁体   中英

Not able to read from powershell argument list

I have a simple dotnet application which i want to execute from powershell. but while i am passing the arguments in powershell my dotnet application is not able to catch those value. I am not sure where the error is. is it in dotnet side or in powershell. dotnet winform

private void Form1_load(object sender, EventArgs e)
    {
        string[] passedArgs = Environment.GetCommandLineArgs();
        foreach(string s in passedArgs)
        {

            textBox1.Text = s.ToString();
        }
    }

powershell script

PS C:\Users\528741> Start-Process  'D:\MVC\PowershellTest\PowershellTest\bin\Debug\PowershellTest.exe' -ArgumentList '/hello'

Thanks, Rosalini

It's difficult to tell without seeing the rest of the application what might be going wrong, as what you've shown us is pretty straightforward. Things to try:

textbox1.Text = "Does a string literal work?";

If the textbox doesn't display your string literal, the issue is in your C# or in the design of your winforms application. Maybe textbox1 isn't visible anymore and you have some other textbox object displayed? Maybe the Form1_load() method isn't being called when you think it is. Did a name get changed somewhere? If that's the case I would suggest recreating the form and pasting your code for the Form1_load() method into there and try again.

If it does display as expected, then I would suspect something with Powershell. Have you tried without Start-Process? Like:

& "D:\MVC\PowershellTest\PowershellTest\bin\Debug\PowershellTest.exe" "I'm argument 1" "This is argument 2" "3rd argument here!"

Also, as your code looks simple enough, are you sure this is the correct path to your executable? Have you rebuilt your solution after making your changes to the code? Simple oversights like this are often the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM