Rename process name
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
Réponses
- 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- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:01
- 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."- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:01
- 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?
- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:00
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
- Marqué comme réponseqwerty53 mercredi 4 novembre 2009 17:31
Toutes les réponses
- 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- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:01
- 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.
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 :)
- 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?
- 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? - 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."- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:01
You can identify processes by using ProcessId.
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....
Is it possible for you to store the id when you create process and kill the process by this id?Use the static method ...
Please read my response above. Thanks.
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."- 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.
- 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." If using the ProcessID is not suitable then I cannot think of anything that will do the trick for you.
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.
Mark the best replies as answers. "Fooling computers since 1971."We want to avoide files as much as we can... - answer to other reply- 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?
- Marqué comme réponseBin-ze ZhaoMSFT, Modérateurlundi 2 novembre 2009 06:00
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!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
- Marqué comme réponseqwerty53 mercredi 4 novembre 2009 17:31
YOU ARE THE MAN!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

