ProcessStartInfo - execute a command from windows service

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);

int exitCode = Process.ExitCode;

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?


Who is Participating?
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.