コード例 #1
0
        public static Tuple <double, double> GetLinxMemoryInGB()
        {
            try
            {
                var results    = OSUtils.StartProcessWithResults("cat", "/proc/meminfo");
                var resultsArr = results.Split("\n".ToCharArray());
                var freeKB     = resultsArr
                                 .FirstOrDefault(x => x.Trim().StartsWith("MemAvailable"))
                                 .Trim()
                                 .Split(" ".ToCharArray(), 2)
                                 .Last() // 9168236 kB
                                 .Trim()
                                 .Split(' ')
                                 .First(); // 9168236

                var totalKB = resultsArr
                              .FirstOrDefault(x => x.Trim().StartsWith("MemTotal"))
                              .Trim()
                              .Split(" ".ToCharArray(), 2)
                              .Last() // 16637468 kB
                              .Trim()
                              .Split(' ')
                              .First(); // 16637468

                var freeGB  = Math.Round((double.Parse(freeKB) / 1024 / 1024), 2);
                var totalGB = Math.Round((double.Parse(totalKB) / 1024 / 1024), 2);

                return(new Tuple <double, double>(freeGB, totalGB));
            }
            catch
            {
                return(new Tuple <double, double>(0, 0));
            }
        }
コード例 #2
0
 public static string GetCurrentUser()
 {
     try
     {
         if (OSUtils.IsWindows)
         {
             return(Win32Interop.GetActiveSessions().LastOrDefault()?.Username);
         }
         else if (OSUtils.IsLinux)
         {
             var users = OSUtils.StartProcessWithResults("users", "");
             return(users?.Split()?.FirstOrDefault()?.Trim());
         }
         throw new Exception("Unsupported operating system.");
     }
     catch
     {
         return("Error Retrieving");
     }
 }
コード例 #3
0
 public static string GetCurrentUser()
 {
     try
     {
         if (OSUtils.IsWindows)
         {
             var session        = CimSession.Create(null);
             var computerSystem = session.EnumerateInstances("root\\cimv2", "CIM_ComputerSystem");
             var username       = computerSystem.FirstOrDefault().CimInstanceProperties["UserName"].Value ?? "";
             return(username as string);
         }
         else if (OSUtils.IsLinux)
         {
             var users    = OSUtils.StartProcessWithResults("users", "");
             var username = users?.Split()?.FirstOrDefault()?.Trim();
             return($"{Environment.UserDomainName}\\{username}");
         }
         throw new Exception("Unsupported operating system.");
     }
     catch
     {
         return("Error Retrieving");
     }
 }