-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
55 lines (50 loc) · 1.86 KB
/
Program.cs
File metadata and controls
55 lines (50 loc) · 1.86 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProcessMonitor
{
internal class Program
{
static void Main(string[] args)
{
// הדפסת שורת כותרת
Console.WriteLine("Id \t Memory \t Name");
// קבלת כל התהליכים הרצים במערכת
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
try
{
// קבלת פרטי התהליך
int processId = process.Id;
string processName = process.ProcessName;
long memorySize = process.PrivateMemorySize64;
// המרת כמות הזיכרון לפורמט קריא
string formattedMemory;
if (memorySize < 1024)
{
formattedMemory = $"{memorySize} Bytes";
}
else if (memorySize < 1024 * 1024)
{
formattedMemory = $"{memorySize / 1024} KB";
}
else
{
formattedMemory = $"{memorySize / (1024 * 1024)} MB";
}
// הדפסת פרטי התהליך
Console.WriteLine($"{processId} \t {formattedMemory} \t {processName}");
}
catch (Exception)
{
// אם קורה Exception, התוכנית ממשיכה לרוץ
Console.WriteLine($"Could not access process {process.Id}");
}
}
}
}
}