Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Powershell中的MessageBox未显示在前面_C#_.net_Powershell_Messagebox - Fatal编程技术网

C# Powershell中的MessageBox未显示在前面

C# Powershell中的MessageBox未显示在前面,c#,.net,powershell,messagebox,C#,.net,Powershell,Messagebox,我使用的应用程序允许我在网络中的设备上运行Powershell脚本,我需要用MessageBox提示用户 我的脚本很好地创建了MessageBox,但我的问题是它总是显示在我的应用程序后面。我在网上尝试了一个解决方案,它建议创建一个属性为Topmost=true的新表单,并将其作为第一个参数传递,但似乎不起作用。有没有什么明显的迹象表明我做错了 Add-Type -AssemblyName PresentationCore,PresentationFramework $top = new-Ob

我使用的应用程序允许我在网络中的设备上运行Powershell脚本,我需要用MessageBox提示用户

我的脚本很好地创建了MessageBox,但我的问题是它总是显示在我的应用程序后面。我在网上尝试了一个解决方案,它建议创建一个属性为Topmost=true的新表单,并将其作为第一个参数传递,但似乎不起作用。有没有什么明显的迹象表明我做错了

Add-Type -AssemblyName PresentationCore,PresentationFramework

$top = new-Object System.Windows.Forms.Form -property @{Topmost=$true}
$Result = [System.Windows.Forms.MessageBox]::Show($top, $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

派生的窗口需要父窗口。如果他们不这样做,他们将落后于其他窗口(正如您所发现的)

以下内容本身与PowerShell无关。。。但这是你需要知道/做的事情,才能到达你想去的地方

还有可能


您不需要使用[System.Windows.Forms.Form]对象,您可以在一行中使用
$this
变量作为第一个参数:

$Result = [System.Windows.Forms.MessageBox]::Show($this, $MessageBody, $MessageTitle, $ButtonType, $MessageIcon)

$this方法不起作用。我不知道你们从哪里获得信息,或者你们是否知道如何在Powershell中编码,但你们提供的信息是错误的。我测试了$this方法,我完全可以向您保证,它在PowerShell中不会以任何方式工作

以下是在PowerShell中真正起作用的唯一方法:

Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('MyMessage','OKOnly,SystemModal,Information', 'HeaderText')
正是SystemModal参数迫使MsgBox占据主导地位,并且无论发生什么情况,它始终保持在顶部

还有另一个有文档记录的方法,Wscript.Shell方法,其他人声称它是有效的,见下文

New-Object -ComObject Wscript.Shell

$wshell.Popup("MyMessage",0,"MyHeader",0 + 16 + 4096)

Where first 0 = No timeout, second 0 = OKOnly, 16 = Critical, 4096 = SystemModal
它也不工作,因为我仍然能够返回到我以前的表单,并在显示MsgBox时对其进行更改,这是我不想要的