How to get OS name on Unix-like systems with C#

1 Answer

0 votes
using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("libc")]
    private static extern int uname(IntPtr buf);

    public static void Main()
    {
        int bufferSize = 8192;
        IntPtr buf = Marshal.AllocHGlobal(bufferSize);

        if (uname(buf) == 0) {
            string sysInfo = Marshal.PtrToStringAnsi(buf);
            Console.WriteLine(sysInfo);
        }
        else {
            Console.WriteLine("Failed to retrieve system information.");
        }

        Marshal.FreeHGlobal(buf);
    }
}


/*
run:

Linux

*/

 



answered Jun 6, 2025 by avibootz

Related questions

1 answer 148 views
1 answer 108 views
1 answer 92 views
1 answer 102 views
1 answer 90 views
1 answer 189 views
...