Convert DOS execution to C# code

  • Thread starter Thread starter Zeya
  • Start date Start date
Z

Zeya

Scenario:
Currently, there is a .lnk file which when executed does the following:
(from Property -> target)
%windir%\system32\cmd.exe /k set a=something&set
b=something2&c:\path\setsenvironment.cmd

Note: the last parameter (C:\path\setsenvironment.cmd).

Executes and waits at the command prompt where the user types a .cmd
file name to execute and complete the whole process.

Eg: Execute

and this takes care of the rest of the processing.

Problem:
How can I do this from C# program? I am required to automate this whole
process using Process. I know how run executables etc using process but
here I am not able to figure this one out.

This is what I tried to do with my existing code:
1. Tried to set environment variables using -
StartInfo.EnvironmentVariables.Add(key, value). EnvironmentVariables
being the ones from the lnk file.

2. WorkingDirectory as where the C:\Path is

Upon starting the process here is what I get:

Exception Details: System.ComponentModel.Win32Exception: The system
cannot find the file specified.

Thanks.
 
The error is because you use "%windir%" and do not expand it.
Try as following:
Process p = new Process();
p.StartInfo.FileName =
Environment.ExpandEnvironmentVariables(@"%windir%\system32\cmd.exe");
p.StartInfo.Arguments = @"/k set a=something&set
b=something2&c:\path\setsenvironment.cmd";
p.Start();

Make sure that the file c:\path\setsenvironment.cmd exists.
1. Tried to set environment variables using -
StartInfo.EnvironmentVariables.Add(key, value). EnvironmentVariables
being the ones from the lnk file.
I don't think that is necessary.
2. WorkingDirectory as where the C:\Path is
WorkingDirectory should be the same as in the .lnk file. If it is not
set in the .lnk file, the n you don't need to set in your program.

Thi
 
Thanks Thi for your response.

This did not work for me. So what I did is created a batch file and am
trying to execute the batch file instead. Batch file works well when I
execute it from command line but when I execute it from the application
nothing happens and the exit code returned is 1, which is an error!

Do you have any suggestion? Thanks again.
 
This did not work for me
It did work on my machine. What is inside setenvironment.cmd? If the
path to setenvironment.cmd contain spaces, you need to put it in double
quotes:
p.StartInfo.Arguments = "/k set a=something&set
b=something2&\"c:\\path\\setsenvironment.cmd\"";
Batch file works well when I
execute it from command line but when I execute it from the application
nothing happens and the exit code returned is 1, which is an error!
Could you post the contents of the .bat file? Maybe you need to set
StartInfo.WorkingDirectory. How did you call the .bat in your code.
This code is what I tested to succesfully call the .NET command prompt:

Process p = new Process();
p.StartInfo.FileName =
Environment.ExpandEnvironmentVariables("%comspec%");
p.StartInfo.Arguments = "/k \"D:\\Program Files\\Microsoft Visual
Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\"";
Console.WriteLine(p.StartInfo.Arguments);
p.Start();

Note I use %comspec% instead of %windir%\system32\cmd.exe to ensure the
correct path on all system.

Hopes it helps,
Thi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top