MSDN > Home page del forum > Visual C# General > Rename process name
Formula una domandaFormula una domanda
 

Con rispostaRename process name

  • martedì 27 ottobre 2009 16.06qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    Hi,

    I have a c# program that runs as an exe from cmd file.

    Now, the problem is that we have more then one cmd file (same contents other then index) that runs this exe. So when those cmd files start they create few processes but with the same name. We need each process have unique name (according to the index for example).

    I know that process name field in Process is read only.

    How do I do this?

     

    Thanks,

    John

Risposte

  • martedì 27 ottobre 2009 16.25Andrey Sidorenko Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    The method is described here https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=66443&lngWId=1
    Visual Basic is used, but you can check the idea and use to re-implement in using C#.

    Thanks,
    Andrey
  • martedì 27 ottobre 2009 18.18Rudedog2ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Use the static method ...

    Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

    ... Now you have a unique Process.ID property value that you can store away to reference later.


    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
  • martedì 27 ottobre 2009 19.44Ray M_ Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    I assume each instance will either have a unique location or parameters being fed to it, run though all processes look for the one you're interested determine what its doing by the parameters and kill if needed?
  • lunedì 2 novembre 2009 14.47Ray M_ Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice

    Something like this ought to do it

    System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("Select * from Win32_Process");
    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
    ManagementObjectCollection oReturnCollection = oSearcher.Get();
    foreach (ManagementBaseObject b in oReturnCollection)
    {
            Console.WriteLine("{0}\t{1}", b.GetPropertyValue("ProcessID"), 
                                          b.GetPropertyValue("CommandLine"));
    }
    

    Be sure to add a reference to System.Managment

    • Contrassegnato come rispostaqwerty53 mercoledì 4 novembre 2009 17.31
    •  

Tutte le risposte

  • martedì 27 ottobre 2009 16.25Andrey Sidorenko Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    The method is described here https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=66443&lngWId=1
    Visual Basic is used, but you can check the idea and use to re-implement in using C#.

    Thanks,
    Andrey
  • martedì 27 ottobre 2009 16.33ScottyDoesKnow Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I highly doubt this is possible due to security reasons. What you could do is make the exe a dll instead, and then create exe's with different names instead of cmd files that call the library with the different indices. Then you'd get the different names.
  • martedì 27 ottobre 2009 16.38Ray M_ Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    While you can't change the acutal process name the code posted uses a sneaky trick, it watches for the task monitor window and changes the text in the listview.  I personally wouldn't use it but have to admit its damn clever :)

  • martedì 27 ottobre 2009 18.12qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Guys. I do not need to see anything in the task manager. It's just I have another program that needs to kill the right process eventualy. But it won't be able to do it because it won't know what process belongs to what index.
    The vb stuff is.....
    Maybe when I start a process in the cmd file I can do a trick?
  • martedì 27 ottobre 2009 18.15Tamer OzMVPMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    You can identify processes by using ProcessId.

    Is it possible for you to store the id when you create process and kill the process by this id?
  • martedì 27 ottobre 2009 18.18Rudedog2ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Use the static method ...

    Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

    ... Now you have a unique Process.ID property value that you can store away to reference later.


    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
  • martedì 27 ottobre 2009 18.39qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    You can identify processes by using ProcessId.

    Is it possible for you to store the id when you create process and kill the process by this id?
    preferably not. because then it will MUST be the same instance of the other process( that does the maintanace). What would happen if it is killed itself? Then the proceses are "orphans".. I could stor it in the registary but....
  • martedì 27 ottobre 2009 18.40qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Use the static method ...

    Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

    ... Now you have a unique Process.ID property value that you can store away to reference later.


    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Please read my response above. Thanks.
  • martedì 27 ottobre 2009 19.11ScottyDoesKnow Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Pick a directory. When one of the exe's starts it can write a text file named for it's index to that directory, inside is it's process id. The other program knows about the directory and can then close each index by PID.
  • martedì 27 ottobre 2009 19.18Rudedog2ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    If using the ProcessID is not suitable then I cannot think of anything that will do the trick for you. 

    Mark the best replies as answers. "Fooling computers since 1971."
  • martedì 27 ottobre 2009 19.33qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    If using the ProcessID is not suitable then I cannot think of anything that will do the trick for you. 

    Mark the best replies as answers. "Fooling computers since 1971."
    Process ID is sutable. What is not is being tied to an instance. So if I save it somewhere and the process (the cleaner) dies then that info is lost.
    We want to avoide files as much as we can... - answer to other reply
  • martedì 27 ottobre 2009 19.44Ray M_ Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    I assume each instance will either have a unique location or parameters being fed to it, run though all processes look for the one you're interested determine what its doing by the parameters and kill if needed?
  • lunedì 2 novembre 2009 12.00qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I assume each instance will either have a unique location or parameters being fed to it, run though all processes look for the one you're interested determine what its doing by the parameters and kill if needed?
    So you are saying that there is away to grab one of the processes (the ones that are named the same) and distinguish between them by the unique parameters that where passed into them? Sounds great! How do I do this? Thank you!
  • lunedì 2 novembre 2009 14.47Ray M_ Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice

    Something like this ought to do it

    System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("Select * from Win32_Process");
    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
    ManagementObjectCollection oReturnCollection = oSearcher.Get();
    foreach (ManagementBaseObject b in oReturnCollection)
    {
            Console.WriteLine("{0}\t{1}", b.GetPropertyValue("ProcessID"), 
                                          b.GetPropertyValue("CommandLine"));
    }
    

    Be sure to add a reference to System.Managment

    • Contrassegnato come rispostaqwerty53 mercoledì 4 novembre 2009 17.31
    •  
  • mercoledì 4 novembre 2009 20.08qwerty53 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice

    Something like this ought to do it

    System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("Select * from Win32_Process");
    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);
    ManagementObjectCollection oReturnCollection = oSearcher.Get();
    foreach (ManagementBaseObject b in oReturnCollection)
    {
            Console.WriteLine("{0}\t{1}", b.GetPropertyValue("ProcessID"), 
                                          b.GetPropertyValue("CommandLine"));
    }
    

    Be sure to add a reference to System.Managment

    YOU ARE THE MAN!