Workaround for bug in mono: Wrong Process.ProcessN

2019-08-09 02:07发布

问题:

If you work with mono and use Process.ProcessName you may get wrong results on some computers.

For example instead of the process name "kwrite" you may get "kdeinit4" (seen on SUSE).

On Ubuntu I have even seen complete bullshit like "kdeinit4;5535948c (deleted)" instead of "kwrite".

Note: On other computers the result may be correct.

If I use Process.MainModule.ModuleName it retruns the same wrong name. And if I use Process.MainModule.FileName it gives the wrong path. Apart from that these commands are EXTREMELY slow.

So whatever I try it is full of bugs. What can I do?

回答1:

The workaround can be done with two lines:

String sProcFile = String.Format("/proc/{0}/comm", proc.Id);
String sProcName = File.ReadAllText(sProcFile).Trim();

It works like a charm on all computers where ProcessName fails.



标签: c# linux mono