简体   繁体   中英

Change process name in C#?

Is it possible to change the name of a currently executing process in C# (or .NET in general)? I believe it isn't possible, but my co-worker is trying to solve a problem under the assumption that it is.

No it is not possible to change the name of the currently executing process. Process names are defined by the name file which was loaded into the operating system for execution at the time execution began. You cannot change this at a later point. This is true for C#, managed and native code.

Yes it is possible... al least under linux. Using this:

[System.Runtime.InteropServices.DllImport("libc")]
// Linux  
private static extern int prctl (int option, byte[] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);

I suppose a dll exists under windows that works in the same way :-)

Process name in Windows is derived from the EXE used to run the file. If it's your process, you could copy your EXE to a temporary folder with a new name, and then run it from there...

This assumes that it'd be running in the same context, etc.

If you start your solution via a batch-file. Then your solution is running within process of this batch-file. In this case process name will be defined by batch-file and not by your solution.

This might be not a direct answer to your question. But it might be a start to find another way around in a similar case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM