【问题标题】:Running powershell from C#从 C# 运行 powershell
【发布时间】:2015-10-13 23:31:22
【问题描述】:

大家好,我是 C# 新手。我知道powershell。我试图弄清楚如何从 c# 程序运行 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

【问题讨论】:

标签: c# powershell


【解决方案1】:

只有 UI 线程可以做 UI 操作。据我所知,您的 PS.Invoke() 创建了自己的线程,我没有找到任何可以让您将其提升到 UI 线程的东西。

【讨论】:

  • 好的,所以我不能在 powershell 中使用任何类型的 UI?我必须将它与 C# gui 交织在一起?
  • 是的,我认为这是人们使用的正常模式。这显示了 C# 捕获 .Invoke() 的输出,然后将其输出:stackoverflow.com/questions/13251076/…
猜你喜欢
  • 2014-03-18
  • 2019-09-30
  • 2021-09-11
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2014-07-23
相关资源
最近更新 更多