【发布时间】:2017-07-18 07:49:28
【问题描述】:
我已完成所有脚本,并在 Visual Studio Enterprise 2015 中编写了整个 C# GUI。
我只需要让按钮在单击时启动特定脚本(Control.OnClick 方法?)。
我尝试寻找示例,但它们非常模糊。
希望的结果如下:
### Begin Code ###
## C# psuedo code here ##
//make C# bunifu Button 1 call script on click of button1
Invoke.OnClick Method (Or Correct option)
//Functional Example of an Invoke.OnClick Method Here doing something here. This is so I can learn and understand.
Place holder
//powershell equivalent
$SOCMonkeyDoStuff = Invoke-Item "C:\Powershell\Scripts\BecomeOneWithCodeMonkeys\Script.ps1
//Button Interaction in Powershell
$Title = "Task Menu"
$Caption = @"
1 - Get Running Services
2 - Get Top Processes
3 - Get Disk Utilization
Q - Quit
Select a choice:
"@
$coll = @()
$a = [System.Management.Automation.Host.ChoiceDescription]::new("&1 Services")
$a.HelpMessage = "Get Running Services"
$a | Add-Member -MemberType ScriptMethod -Name Invoke -Value {Get-service | where {$_.status -eq "running"}} -force
$coll+=$a
而且我知道,是的,这段代码已经可以运行了,为什么不直接在 powershell 中执行呢?原因是我计划在不久的将来将它变成一个平台无关的工具,这样 Linux 和 Windows 都可以使用它,并且我希望尽可能保持供应商中立。此外,我计划将其导出为 .exe,以便可以在任何地方安装。
【问题讨论】:
-
您可以使用 "Process.Start("full path here")... 作为快速 hack 调用脚本
-
@BugFinder 好的,所以我的故障是这样的。我第一次接触 Visual Studio……大约 14 小时前。我第一次做一个看起来不错的 UI 是在我安装 VS Ent 的时候。 2015. 我不知道将 process.start 代码放在哪里,尽管我相信正确的代码是:'code' process.start(""C:\Powershell\Scripts\BecomeOneWithCodeMonkeys\Script.ps1") 'code ' 对吗?
-
您需要在该参数前面添加一个 @ 或使用双 \\ 或者是您的选择。你把 Process.Start 放在你的按钮 click .. :)
-
@Mhd 我确实找到了那篇文章,这是一篇令人难以置信的文章,所以感谢您分享。对于没有包括我迄今为止使用的所有资源,我深表歉意。我能够按照说明导入所需的所有参考资料,并找到 system.management.automation.dll 文件。我的问题是这段代码,我不知道把它放在哪里或如何让它工作。我知道命名空间声明了实例,程序定义了类,但是我该如何将它放入 bunifuflatbutton1,或者我可以吗?我不知道。哈哈
标签: c# visual-studio powershell imgur