ProcessStartInfo - execute a command from windows service

Posted on 2006-05-13
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);

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?


Question by:carlostriassi
    1 Comment
    LVL 32

    Accepted Solution

    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.


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    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 …

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now