如何使用C#安装Chocolatey
本文关键字:Chocolatey 安装 何使用 | 更新日期: 2023-09-27 17:53:09
我尝试从网站运行命令行指令
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
}
}
}
}
通过这篇文章。
您可以在以下位置验证安装:C:''ProgramData''chocolatey或运行cinst。如果你已经安装了它,只需将其重命名为chocolatey.old,这将允许你进行测试。完成后,删除新的"chocolatey"文件夹,并将"chocolaty.old"文件夹重新命名。
安装巧克力软件:
private async Task ChocolateyDownload(string software)
{
await RunCommandHidden("cd '"C:''ProgramData''chocolatey'"'nchoco.exe install " + software + " -y");
}
用法:wait ChocolateyDownload(您的软件名称未安装choco(;
获取巧克力:
private async Task InstallChocolatey()
{
if (!Directory.Exists("C:''ProgramData''chocolatey"))
{
await RunCommandHidden(
"@'"%SystemRoot%''System32''WindowsPowerShell''v1.0''powershell.exe'" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command '" [System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))'" && SET '"PATH=%PATH%;%ALLUSERSPROFILE%''chocolatey''bin'"");
}
}
用法:await InstallChocolatey((;
运行命令隐藏功能:
private bool Exit = false;
public async Task RunCommandHidden(string Command)
{
Random dew = new Random();
int hui = dew.Next(0000, 9999);
string[] CommandChut = { Command };
File.WriteAllLines(System.Environment.GetEnvironmentVariable("USERPROFILE") + "''Documents''RunCommand" + hui + ".bat", CommandChut);
Process C = new Process();
C.StartInfo.FileName = System.Environment.GetEnvironmentVariable("USERPROFILE") + "''Documents''RunCommand" + hui + ".bat";
C.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
C.EnableRaisingEvents = true;
C.Exited += C_Exited;
C.Start();
while (Exit == false)
{
await Task.Delay(10);
}
Exit = false;
File.Delete(System.Environment.GetEnvironmentVariable("USERPROFILE") + "''Documents''RunCommand" + hui + ".bat");
}
private void C_Exited(object sender, EventArgs e)
{
Exit = true;
}