gpt4 book ai didi

c# - 如何使用 C# 安装 Chocolatey?

转载 作者:行者123 更新时间:2023-11-30 21:57:35 25 4
gpt4 key购买 nike

我尝试从网站运行命令行指令

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))*

在管道中

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(commandToRun);
Collection<PSObject> results = pipeline.Invoke();

// close the runspace
runspace.Close();

并通过调用流程

Process.Start("@powershell -NoProfile -ExecutionPolicy unrestricted -Command \"iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))\" && SET PATH=%PATH%;%ALLUSERSPROFILE%\\chocolatey\\bin")

最佳答案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CInstInst
{
class Program
{
static void Main(string[] args)
{

string command = @"@powershell -NoProfile -ExecutionPolicy Bypass -Command ""iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin";
ExecuteCommandSync(command);
}

public static void ExecuteCommandSync(object command)
{
try
{
// create the ProcessStartInfo using "cmd" as the program to be run,
// and "/c " as the parameters.
// Incidentally, /c tells cmd that we want it to execute the command that follows,
// and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
}
catch (Exception objException)
{
// Log the exception
}
}
}
}

Via this article.

您可以在此处验证安装:C:\ProgramData\chocolatey 或运行 cinst。如果您已经安装了它,只需将其重命名为 chocolatey.old,这将允许您进行测试。完成后 - 删除新的“chocolatey”文件夹并将“chocolatey.old”文件夹重新命名。

关于c# - 如何使用 C# 安装 Chocolatey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30623118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com