View Full Version : C# Pulling Process ID

07-07-2008, 03:51 PM

I have a log on script to launch an application. Mostly it maps network drives on the local computer that are hard coded into the application (IE The application was not designed for a network based install).

I want to build a *.exe that reads from a SQL Database for permissions for a given user. (IE: The application development team needs to have the testing environment mapped).

Currently, I have a *.bat file searches a network folder using the IF EXISIT function. If there is a file in that folder named by the user's login ID, then they extra drive is mapped. If there is not one, then the extra drive is not mapped.

In the batch script I call the NET USE function.
1. First I delete the old drive mapping.
2. Then I map that drive letter to the correct location.

I have the C# code working. My problem is that is runs the code to quickly. The old drive does not completely unmap prior to the new mapping command being executed causing Windows Explorer to do weird things (It shows that it is still mapped to the old location, or when the other command is completed it doesn’t show at all, until I call it in the address bar).

This code using the System.Diagnostics

//Delete Old Drive Mappings
Process.Start(@"C:/WINDOWS/System32/net.exe", @"use "+driveLetter+" /delete");

//Map Drives
Process.Start(@"C:/WINDOWS/System32/net.exe", @"use "+driveLetter+" "+networkLocation);

I want to place a dynamic wait in between these commands. Obtain Process ID for the process that just started (Delete Old Drive Mappings). Wait until process is shutdown. Execute map command.

I am not sure how to obtain the process ID for the process that just started.


07-07-2008, 04:35 PM
Hmm. I ran your code and everything seemed fine when I tried to access the mapped drives. At any rate, look into Process.Id (http://msdn.microsoft.com/en-us/library/system.diagnostics.process.id%28VS.71%29.aspx) and System.Threading.Thread.Sleep (http://msdn.microsoft.com/en-us/library/system.threading.thread.sleep.aspx):

Process p = Process.Start(@"C:/WINDOWS/System32/net.exe", @"use "+driveLetter+" /delete");
// p.Id should have the generated id