About Powershell in Windows Forms

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,
The question has already arisen about Run PowerShell in WinForms, which intrinsically is questionable, but sometimes gives a good help in giving a results quickly.

I proposed this :
Execute PowerShell from a C# code in a WinForms project:
            Process p = new Process();
            p.StartInfo = new ProcessStartInfo("PowerShell", @"D:\Scripts\ReduceDirWindows.ps1");
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            System.Threading.Thread.Sleep(1000);

I use it in a project of mine in .Net 6, and it runs pretty good.

Now, as discussed in another thread, it appears the Visual Studio toolbox stays empty in this project, representing a menace about maintaining the project.

I discover that in a .Net Framework 4.7.2, for the moment being there is still a toolbox.

I have a terrible tendency to accord some credit to people about not being completely stupid, so I think perhaps this will keep on working a little time.

Now, here is why I open a new thread about this. In the .Net 6 project the code works exactly as expected, the file is read without anything noticeable on the screen. But in .Net 4.7.2, the PowerShell window is opened. Of course it stays completely black except the title, as the scripts writes nothing on the console. .Net Framework seems to ignore p.StartInfo.CreateNoWindow = true;

Does anybody have any idea about this difference of execution?
I copied/pasted the code from a project to the other, just increasing a little the delay as the execution seemed to take a little more time as appeared with not up-to-date data, and displaying a window on the screen can be what takes more time.
 
Solution
Now, as discussed in another thread, it appears the Visual Studio toolbox stays empty in this project, representing a menace about maintaining the project.

I discover that in a .Net Framework 4.7.2, for the moment being there is still a toolbox.
I don't understand. What does Visual Studio have to do with running a WinForms program and that WinForms program tries to run PowerShell. Can you show a screenshot of this "toolbox"?

Anyway the difference in behavior is in the documentation for your CreateNoWindow:
If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created

And so if we jump to...
Now, as discussed in another thread, it appears the Visual Studio toolbox stays empty in this project, representing a menace about maintaining the project.

I discover that in a .Net Framework 4.7.2, for the moment being there is still a toolbox.
I don't understand. What does Visual Studio have to do with running a WinForms program and that WinForms program tries to run PowerShell. Can you show a screenshot of this "toolbox"?

Anyway the difference in behavior is in the documentation for your CreateNoWindow:
If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created

And so if we jump to the documentation for UseShellExecute, we see:
true if the shell should be used when starting the process; false if the process should be created directly from the executable file. The default is true on .NET Framework apps and false on .NET Core apps.
 
Solution
I don't understand. What does Visual Studio have to do with running a WinForms program and that WinForms program tries to run PowerShell. Can you show a screenshot of this "toolbox"?
Well, if you like 😊
1659060446752.png

But ... when you asked it, it was empty. I had to close the project, reopen it, see the empty toolbox, except printing tools, expand the printing tools, see something else to expand, and eventually "all the elements".
Anyway the difference in behavior is in the documentation for your CreateNoWindow:


And so if we jump to the documentation for UseShellExecute, we see:
Oh, just what had to be seen 😊
Well, it seems I mixed up somewhat about the quotations, but I imagine you remember what you said.
Thank you, quick and precise answer.
 
UseShellExecute: The default is true on .NET Framework apps and false on .NET Core apps.

I understood I had to put it to true, in fact I had to put it to false.
Strange, but anyway there are only two possibilities ...
And eventually I could avoid the PowerShell window.

Oh, understood: I was migrating from Core to Framework, and reacting as if I did reverse.
 
Back
Top Bottom