471,319 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Convert DOS execution to C# code

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.

Dec 2 '05 #1
4 6016
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

Dec 2 '05 #2
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.

Dec 2 '05 #3
>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

Dec 3 '05 #4
I have posted another message here which has the code info:

http://groups.google.com/group/micro...ecf4ee104950dc

Dec 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nicolas Guilhot | last post: by
30 posts views Thread by John Carroll | last post: by
5 posts views Thread by Andy Sutorius | last post: by
4 posts views Thread by sweety | last post: by
14 posts views Thread by rtillmore | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.