Click here to Skip to main content
15,742,357 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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.
Patrice T 21-Sep-15 16:30pm    
Tried Google ?
JamesHadleyChase 21-Sep-15 16:33pm    
tried, in vain.

1 solution

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, like Directory or System.

You then use the returned paths with Path.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.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900