Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2006
    Thanked 0 Times in 0 Posts

    C# Pulling Process ID


    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.


  2. #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Thanked 40 Times in 40 Posts
    Hmm. I ran your code and everything seemed fine when I tried to access the mapped drives. At any rate, look into Process.Id and System.Threading.Thread.Sleep:
    Process p = Process.Start(@"C:/WINDOWS/System32/net.exe", @"use "+driveLetter+" /delete");
    // p.Id should have the generated id


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts