How to use net.exe in a Windows Service

I'm tyring to use net.exe within a Windows Service.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

The following code was found at:

http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassstandarderrortopic.asp

 

Process myProcess = new Process();

string[] args = {"z: /delete"};  //a previously mapped drive using using net.exe through cmd

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use "+ args[0]);

 

myProcessStartInfo.UseShellExecute = false;

myProcessStartInfo.RedirectStandardError = true;

myProcess.StartInfo = myProcessStartInfo;

myProcess.Start();

 

StreamReader myStreamReader = myProcess.StandardError;

// Read the standard error of net.exe and write it on to console.

eventLog1.WriteEntry( myStreamReader.ReadLine());

myProcess.Close();

 

This code always generates the error:

The network connection could not be found

 

I realize this may have something to do with the security permissions of the service, but I have no idea how to change them.

 

I know how to modify the ServiceAccount, which I set to LocalSystem.

 

Any info would be greatly appreciated.

 

Thanks

 



Answer this question

How to use net.exe in a Windows Service

  • help_m_with_extern_linkage

    The potential security issue is that Services running under the local system account typically don't  have network privileges, so they can't refer to mapped drives.

    The other potential issue is that mapped drives are user-specific. If the drive was mapped from some user account, a Service running under local system cannot use it because the drive mapping is for that user, not all users off the system.



  • How to use net.exe in a Windows Service