.NET Core 获取系统平台环境信息、进程信息

2020年2月26日 5697点热度 6人点赞 0条评论
内容目录

.NET Core 中没有获取 Linux 的 CPU使用率等信息的功能,死心吧。

        static void Main(string[] args)
        {
            Console.WriteLine("系统运行情况");
            var proc = Process.GetCurrentProcess();
            var mem = proc.WorkingSet64;
            var cpu = proc.TotalProcessorTime;
            Console.WriteLine("当前进程已使用内存:{0},CPU运行时间:{1}",
                mem / 1024.0, cpu.TotalMilliseconds);
            Console.WriteLine($"当前进程物理内存量:kb { Environment.WorkingSet / 1204}");

            Console.WriteLine("\n系统运行平台:");
            Console.WriteLine("运行框架:" + RuntimeInformation.FrameworkDescription);
            Console.WriteLine("操作系统:" + RuntimeInformation.OSDescription);
            Console.WriteLine("操作系统版本:" + Environment.OSVersion.ToString());
            Console.WriteLine("平台架构:" + RuntimeInformation.OSArchitecture);

            //
            Console.WriteLine();
            Console.WriteLine("-- 运行环境 --");

            //  <-- Keep this information secure! -->
            Console.WriteLine("机器名称: " + Environment.MachineName);
            Console.WriteLine("用户网络域名:" + Environment.UserDomainName);
            //  <-- Keep this information secure! -->
            string[] drives = Environment.GetLogicalDrives();
            Console.WriteLine("分区磁盘: {0}", String.Join(", ", drives));
            Console.WriteLine("系统目录:" + Environment.SystemDirectory);

            Console.WriteLine("系统已运行时间(毫秒):" + Environment.TickCount);

            //  <-- Keep this information secure! -->

            Console.WriteLine("是否在交互模式中运行:" + Environment.UserInteractive);

            //  <-- Keep this information secure! -->
            Console.WriteLine("当前关联用户名:" + Environment.UserName);

            Console.WriteLine("Web程序核心框架版本:" + Environment.Version.ToString());

            string querySystemDrive = "系统盘位置:%SystemDrive%";
            string querySystemRoot = "系统根目录:%SystemRoot%";
            Console.WriteLine(Environment.ExpandEnvironmentVariables(querySystemDrive));
            Console.WriteLine(Environment.ExpandEnvironmentVariables(querySystemRoot));

            Console.WriteLine("\n系统变量列表: ");
            IDictionary environmentVariables = Environment.GetEnvironmentVariables();
            foreach (DictionaryEntry de in environmentVariables)
            {
                Console.WriteLine("  {0} = {1}", de.Key, de.Value);
            }

            Console.ReadKey();
        }

痴者工良

高级程序员劝退师

文章评论