将环境变量传递给CMD提示符 [英] Passing Environment variables to the CMD prompt
问题描述
我有这个基本问题,(可能是傻!!)
我我试图从Win 7和XP上的运行对话框运行批处理文件。
目前,在运行窗口,我输入cmd.exe / kc:\ usersrs \ myuser \Desktop\mybatch.bat
但是,我希望不要输入位于桌面上的批处理文件的完全限定路径使用系统环境变量。
对于实例:cmd.exe / k [桌面] \ mybatch.bat
而不是cmd.exe / kc :\ usersrs \ myuser \Desktop \ mybatch.bat
同样,cmd.exe / k [SystemFolder] \ mybatch.bat代替ofcmd.exe /kc:\windows\system\mybatch.bat
现在,如果我继续使用[桌面]或者在运行窗口中的[SystemFolder],它抱怨不知道任何一个系统文件夹。
请指导我。
提前致谢。
Hi,
I have this basic question, (probably stupid !!)
I am trying to run a batch file from the "Run dialog" on Win 7 and XP.
Currently, at the run window, I am typing in "cmd.exe /k c:\users\myuser\Desktop\mybatch.bat"
However, Instead of typing in the fully qualified path to the batch file located on desktop, I wish to use the system environment variable.
For Instance : "cmd.exe /k [Desktop]\mybatch.bat"
instead of "cmd.exe /k c:\users\myuser\Desktop\mybatch.bat"
similarly, "cmd.exe /k [SystemFolder]\mybatch.bat" instead of "cmd.exe /k c:\windows\system\mybatch.bat"
Now, If I proceed by using [Desktop] or [SystemFolder] in the Run Window, it complains of not knowing either of the systemfolder.
Please guide me.
Thanks in advance.
推荐答案
首先,这些不是环境变量。默认情况下这些名称不存在,除非你有一些设置它们的登录脚本。
你应该做的是组装完全限定的路径到这些文件使用已知的起点。您在示例中使用的不是环境变量,而是更接近Environment.SpecialFolder枚举中的名称,找到 here [ ^ ]。
你可以在 Environment.GetFolderPath() [ ^ ]获取指定文件夹的完全限定路径,例如目录
或系统
。
然后使用返回的路径和Path.Combine()
来构建要启动的目标文件的路径。然后,将生成的路径传递给Process代码以启动命令提示符。
First, those are not environment variables. Those names don't exist by default, unless you've got some login script that are setting them.
What you SHOULD be doing is assembling the fully qualified paths to these files using known starting points. What you're using in your examples are not environment variables but are much closer to the names in the Environment.SpecialFolder enum, found here[^].
You can use those in the Environment.GetFolderPath()[^] to get the fully qualified paths to the folder specified, likeDirectory
orSystem
.
You then use the returned paths withPath.Combine()
to build the path to your target file you want to launch. You then pass the resulting path to your Process code to launch the command prompt.
这篇关于将环境变量传递给CMD提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!