[英]Running powershell from C#
大家好,我是C#的新手。 我知道powershell。 我試圖弄清楚如何從ac#程序運行Powershell腳本。 以下是我自己的測試程序。 但是,當我嘗試放入更復雜的腳本時,其中可能包含應該彈出的GUI窗口,似乎沒有運行。 我是否在解決這個問題的正確道路上? 我還注意到,需要使用每行后的分號才能在C#中正確運行腳本。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (PowerShell PS = PowerShell.Create())
{
PS.AddScript("$file = \"C:\\test\\csharp.txt\";"+
"Set-Content -path $file -value \"testc\";");
PS.Invoke();
}
}//end of main
}//end of class
}//end of namespace
僅UI線程可以執行UI操作。 據我所知,您的PS.Invoke()創建了自己的線程,但是我找不到任何可以將其提升到UI線程的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.