CMD, C# 和 Java 不允许我在同一行上使用设置的环境变量。

huangapple go评论89阅读模式
英文:

CMD, C# & Java won't let me use environment variables set on the same line

问题

在批处理文件中,您可以使用以下格式提示将值设置到环境变量中:set /p TestVar=请输入要保存的字符串! 并且可以使用 %TestVar% 进行检索。然而,当我尝试在 Java 或 C# 中执行类似操作并尝试立即使用时,却无法正常工作:

对于 C# 的示例代码如下:

cmd.exe /K "set /p TestVar=请输入要保存的字符串! && ECHO %TestVar%"

上述代码只会输出文字字面量 %TestVar%,而不是设置给 TestVar 变量的字符串。

我希望在 Java 和 C# 中使用这种方式来设置环境变量(我不想使用类似 https://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c 中提到的内置方法)。

对于 Java 的示例代码如下:

public class VisibleCMD {
    public static void main(String[] args) {
        try {
            Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"set /p TestVar=请输入要保存的字符串! && ECHO %TestVar%\"");
        } catch (Exception e) {
            System.out.println("error");
        }
    }
}

上述代码只会输出 %TestVar%,而不是设置给 TestVar 的字符串,就好像根本没有将其解析为变量。

对于 C# 的示例代码如下:

using System;
using System.Diagnostics;

namespace VisibleCMD
{
    class Program
    {
        static void Main(string[] args)
        { 
            Process.Start("CMD.exe", "/K set /p TestVar=请输入要保存的字符串! && ECHO %TestVar%");
        }
    }
}
英文:

In a batch file, you can prompt for values to environment variables using the format of set /p TestVar=Give me a string to save! and retrieve it using %TestVar%. However, when I try to do this in Java or C# and trying to immediately use it does not work:

cmd.exe /K "set /p TestVar=Give me a string to save! && ECHO %TestVar%"

the above code also only ECHOs the literal string "%TestVar%" rather than the string that was set to the TestVar variable.


I want to use that to set environment variables in Java and C# (and I don't want to use built in stuff like https://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c)

For a Java example,

public class visibleCMD {
	public static void main(String[] args) {
		try {
			Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"set /p TestVar=Give me a string to save! && ECHO %TestVar%\"");
		} catch (Exception e) {
			System.out.println("error");
		}
	}
}

the above code only ECHOs %TestVar%, rather than the string that was set to TestVar, almost as if it just isn't being parsed as a variable at all.

For a C# example,

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

namespace VisibleCMD
{
    class Program
    {
        static void Main(string[] args)
        { 
            System.Diagnostics.Process.Start("CMD.exe", 
                "/K set /p TestVar=Give me a string to save! && ECHO %TestVar%");
        }
    }
}

答案1

得分: 2

问题出在您的命令行上,而不是您正在使用的语言上。如果您希望在设置变量的同一行上访问变量的值,您必须启用延迟扩展。传递 /V 开关将启用变量的延迟扩展,使用 ! 而不是 % 将获取变量的当前值:

Process.Start("CMD.exe", 
    "/V /K set /p TestVar=Give me a string to save! && ECHO !TestVar!");
英文:

The problem is with your command line, not with the language you're using. You have to enable delayed expansion if you want to access the variable's value on the same line as it's set. Passing the /V switch will enable delayed expansion of the variable, and using ! instead of % will get the current value of the variable:

Process.Start("CMD.exe", 
    "/V /K set /p TestVar=Give me a string to save! && ECHO !TestVar!");

huangapple
  • 本文由 发表于 2020年10月9日 06:35:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64271566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定