我正在尝试创建一个要从命令行中执行的命令,该命令将从外部源(heroku)中提取环境变量并更新该命令提示符的环境。我最擅长的语言是 C#,所以我决定编写一个控制台应用程序来做到这一点。我已成功从 heroku 获取值,但无法为控制台设置环境变量。
以下是我尝试过的一些选项,但没有一个有效。也许 .NET 甚至不可能
foreach (var kvp in kvps)
{
// Works for user vars, but doesn't update the running console
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value, EnvironmentVariableTarget.User);
// Seems to have no effect. My guess is it only applies to the C# app itself, not the console.
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value, EnvironmentVariableTarget.Process);
// Opens a bunch of windows but doesn't accomplish anything
new Process
{
StartInfo = new ProcessStartInfo(@"cmd.exe", "set " + kvp.Key + "=" + kvp.Value)
}.Start();
// Throws "file not found" exception
new Process
{
StartInfo = new ProcessStartInfo(@"set", kvp.Key + "=" + kvp.Value)
}.Start();
}