473,408 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 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 6199
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
5
by: Eidolon | last post by:
I have a class which inherits from System.Data.DataTable. The code is basically (property procs omitted for clarity): Public Class DataTable() Inherits System.Data.DataTable Public Property...
0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
30
by: John Carroll | last post by:
Does anyone have a function or procedure for converting integers to character strings? Thank you, John
5
by: Andy Sutorius | last post by:
Hi, I am attempting to convert this vb function to csharp but I am getting stuck on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says "method name expected" and underlines...
2
by: Steve Wilkinson | last post by:
I've just started using managed C++ with VS2005, so please forgive my ignorance. I'm investigating producing a managed wrapper for some functionality of the Windows Media Format SDK. I have the...
4
by: sweety | last post by:
Dear all, Kindy help to convert the char* ( LPCSTR) to WCHAR*(LPCWSTR). Would be great if you tell if any function does this job in VC++. Quick response will be greatfull...as its blocked... ...
27
by: fdu.xiaojf | last post by:
Hi, String formatting can be used to converting an integer to its octal or hexadecimal form: '307' 'c7' But, can string formatting be used to convert an integer to its binary form ?
2
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.