Asked  7 Months ago    Answers:  5   Viewed   264 times

I was looking a lot for method to get parent process in .NET, but found only P/Invoke way.



This code provides a nice interface for finding the Parent process object and takes into account the possibility of multiple processes with the same name:


Console.WriteLine("ParentPid: " + Process.GetProcessById(6972).Parent().Id);


public static class ProcessExtensions {
    private static string FindIndexedProcessName(int pid) {
        var processName = Process.GetProcessById(pid).ProcessName;
        var processesByName = Process.GetProcessesByName(processName);
        string processIndexdName = null;

        for (var index = 0; index < processesByName.Length; index++) {
            processIndexdName = index == 0 ? processName : processName + "#" + index;
            var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
            if ((int) processId.NextValue() == pid) {
                return processIndexdName;

        return processIndexdName;

    private static Process FindPidFromIndexedProcessName(string indexedProcessName) {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
        return Process.GetProcessById((int) parentId.NextValue());

    public static Process Parent(this Process process) {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
Tuesday, June 1, 2021
answered 7 Months ago

Orphaned children are always adopted by init. There is no Unix way of changing the parent to some non-init process.

As of Linux 3.4 this is no longer strictly true. There's still no portable Unix way of doing this but as Andy Lutomirski points out Linux 3.4 adds PR_SET_CHILD_SUBREAPER for prctl.

In effect, a subreaper fulfills the role of init(1) for its descendant processes.

Monday, July 5, 2021
answered 6 Months ago

You have to get the host and path separately.

Tuesday, July 27, 2021
answered 5 Months ago

With C# 5.0 there is a new, well-hidden feature that enables this.

Caller Info attributes

Note Apparently, there is also the Microsoft BCL Portability Pack 1.1.3 Nuget package so you can use the Caller Info Attributes in .NET 4.0.

What this does, is make your optional parameters magically have caller-dependent default values. It has

  • CallerFilePathAttribute Full path of the source file that contains the caller. This is the file path at compile time.
  • CallerLineNumberAttribute Line number in the source file at which the method is called.
  • CallerMemberNameAttribute Method or property name of the caller. See Member Names later in this topic.

It has some pretty nifty features:

  • Caller Info values are emitted as literals into the Intermediate Language (IL) at compile time.
  • Unlike the results of the StackTrace property for exceptions, the results aren't affected by obfuscation.

The documentation sample looks like this:

// using System.Runtime.CompilerServices 
// using System.Diagnostics; 

public void DoProcessing()
    TraceMessage("Something happened.");

public void TraceMessage(string message,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    Trace.WriteLine("message: " + message);
    Trace.WriteLine("member name: " + memberName);
    Trace.WriteLine("source file path: " + sourceFilePath);
    Trace.WriteLine("source line number: " + sourceLineNumber);

// Sample Output: 
//  message: Something happened. 
//  member name: DoProcessing 
//  source file path: c:UsersusernameDocumentsVisual Studio 2012ProjectsCallerInfoCSCallerInfoCSForm1.cs 
//  source line number: 31 
Friday, July 30, 2021
answered 5 Months ago

For this type of scenario, do not use a String directly. Instead switch the type to be an IntPtr value and use Marshal.PtrToStringAuto/Ansi/Uni as appropriate. In this case, since your native code uses char*, PtrToStringAnsi is the best choice.

struct ManagedStruct {
  IntPtr s;
  public string sAsString { get { return Marshal.PtrToStringAnsi(s); } }
Friday, August 6, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :