Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ProcessStartInfo - execute a command from windows service

Posted on 2006-05-13
1
Medium Priority
?
929 Views
Last Modified: 2012-05-05

I run this from dos prompt and it works

c:\windows\system32\cmd.exe /c net use \\win_xp_pro\IPC$ /user:administrator 1234

open a network connection.

I have Windows Service C# program that does the following:

string command = string.Format (@"net use \\{0}\IPC$ /user:{1} {2}", "win_xp_pro", "administrator", "1234");

ProcessStartIfo ps = new ProcessStartInfo();
ps.Filename = System.Environment.GetEnvironmentVariable ("COMSPEC");
ps.Arguments = "/C " + command;
ps.UseShellExecute = false;
ps.CreateNoWindow = true; // try both true or false
ps.RedirectStardardOutput = true;

Process process = Process.Start(ps);
process.WaitForExit();

int exitCode = Process.ExitCode;
process.Close();


It always return exit code = 2, which means File not found.

This program above work on a windows application, but I'm having trouble running as a windows service. Eventually, I want to create a batch file that establish connection, do some stuff, and close connection. I'll modify the above code to execute the batch file.


Any ideas?

Thanks,
Carlos.



0
Comment
Question by:carlostriassi
1 Comment
 
LVL 32

Accepted Solution

by:
jhance earned 750 total points
ID: 16673320
Probably the path to the NET.EXE command is not set.  To make the above work, you should specify the complete pathname.  The default location is C:\WINDOWS\SYSTEM32\NET.EXE but remember that the C:\WINDOWS may differ from system to system.  So you should determine the proper Windows folder.  The SYSTEMROOT environment variable is one way.  Use the .NET Environment.SystemDirectory property in .NET.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question