-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
180 lines (156 loc) · 5.93 KB
/
Program.cs
File metadata and controls
180 lines (156 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Management;
using System;
class Program
{
static List<SoftwareInfo> installedSoftware = new List<SoftwareInfo>();
static void Main()
{
try
{
Console.Title = "System Info Fetcher";
Console.WriteLine("Hello! Here is your info:");
WriteBasicSystemInfo();
Console.WriteLine($"\n Drives info\n");
WriteDrivesInfo();
Console.WriteLine("\n Installed programs (NAME - VERSION)\n");
WriteInstalledSoftware();
Console.WriteLine("\n Runtime\n");
WriteRuntimeInfo();
}
catch (Exception ex)
{
Console.WriteLine($"Error during fetching data: {ex}");
}
Console.WriteLine("\nPress ENTER to exit");
Console.ReadLine();
}
private static List<SoftwareInfo> GetInstalledSoftwareList(RegistryKey registryKey, string registryPath)
{
List<SoftwareInfo> softwares = new List<SoftwareInfo>();
using (RegistryKey key = registryKey.OpenSubKey(registryPath))
{
if (key != null)
{
foreach (string subkeyName in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkeyName))
{
try
{
string? displayName = subkey.GetValue("DisplayName")?.ToString();
string displayVersion = subkey.GetValue("DisplayVersion").ToString();
if (!string.IsNullOrEmpty(displayName))
{
softwares.Add(new SoftwareInfo
{
DisplayName = displayName,
DisplayVersion = displayVersion
});
}
}
catch (Exception) { }
}
}
}
}
return softwares;
}
private static object GetOSInstallDate()
{
object installDateObj;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
{
if (key != null)
{
installDateObj = key.GetValue("InstallDate");
}
else
{
installDateObj = 0;
}
}
if (installDateObj == null)
{
return 0;
}
return installDateObj;
}
private static void WriteBasicSystemInfo()
{
int installDateUnix = (int)GetOSInstallDate();
DateTime installDate = DateTimeOffset.FromUnixTimeSeconds(installDateUnix).DateTime;
TimeSpan upTimeTS = TimeSpan.FromMilliseconds(Environment.TickCount64);
Console.WriteLine($"OS: {Environment.OSVersion}");
Console.WriteLine($"OS installation date: {installDate.Day:00}.{installDate.Month:00}.{installDate.Year} {installDate.Hour:00}:{installDate.Minute:00}");
Console.WriteLine($"Machine name: {Environment.MachineName}");
Console.WriteLine($"User name: {Environment.UserName}");
Console.WriteLine($"Is x64 system: {Environment.Is64BitOperatingSystem}");
Console.WriteLine($"System directory: {Environment.SystemDirectory}");
Console.WriteLine($"System up time: {upTimeTS.Hours:00}:{upTimeTS.Minutes:00}:{upTimeTS.Seconds:00}");
}
private static void WriteDrivesInfo()
{
var drives = System.IO.DriveInfo.GetDrives();
for (int i = 0; i < drives.Length; i++)
{
try
{
Console.WriteLine($"{i}. Name: {drives[i].Name}");
Console.WriteLine($" Total size: {Tools.GetReadableStorageSize(drives[i].TotalSize)} ({drives[i].TotalSize} bytes)");
Console.Write($" Free space: {Tools.GetReadableStorageSize(drives[i].TotalFreeSpace)} ({drives[i].TotalFreeSpace} bytes)");
Console.WriteLine($" {(double)drives[i].TotalFreeSpace / drives[i].TotalSize * 100:00.0} %");
Console.WriteLine($" Type: {drives[i].DriveType}");
Console.WriteLine($" Format: {drives[i].DriveFormat}");
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine($"Problem with {i} drive: {e.Message}");
}
}
}
private static void WriteInstalledSoftware()
{
installedSoftware.AddRange(GetInstalledSoftwareList(Registry.LocalMachine,
@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"));
foreach (var software in installedSoftware)
{
Console.WriteLine($" {software.DisplayName} - {software.DisplayVersion}");
}
}
private static void WriteRuntimeInfo()
{
Console.WriteLine($"Processor architecture: {RuntimeInformation.ProcessArchitecture}");
Console.WriteLine($"Current framework: {RuntimeInformation.FrameworkDescription}");
Console.WriteLine($"Runtime identifier: {RuntimeInformation.RuntimeIdentifier}");
}
}
public class Tools
{
public static string GetReadableStorageSize(long sizeInBytes)
{
if (sizeInBytes >= 1024 && sizeInBytes < 1048576)
{
return $"{sizeInBytes / 1024} KiB";
}
else if (sizeInBytes >= 1048576 && sizeInBytes < 1073741824)
{
return $"{sizeInBytes / 1048576} MiB";
}
else if (sizeInBytes >= 1073741824 && sizeInBytes < 1099511627776)
{
return $"{sizeInBytes / 1073741824} GiB";
}
else
{
return $"{sizeInBytes} B";
}
}
}
class SoftwareInfo
{
public string DisplayName { get; set; }
public string DisplayVersion { get; set; }
}