-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.ListRunning.cs
More file actions
50 lines (45 loc) · 1.9 KB
/
Program.ListRunning.cs
File metadata and controls
50 lines (45 loc) · 1.9 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
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace CSharpScriptRunner
{
static partial class Program
{
static async Task ListRunning()
{
WriteLine("Process ID RT Arguments");
WriteLine("---------- --- ---------");
using var thisProcess = Process.GetCurrentProcess();
var runtimesDir = Path.GetDirectoryName(thisProcess.MainModule.FileName); // bin
runtimesDir = Path.GetDirectoryName(runtimesDir); // x64
runtimesDir = Path.GetDirectoryName(runtimesDir); // vX.Y.Z
foreach (var runtimeDir in Directory.EnumerateDirectories(runtimesDir))
{
var dir = Path.Combine(runtimeDir, "bin", "running");
if (!Directory.Exists(dir))
continue;
var rt = Path.GetFileName(runtimeDir);
foreach (var file in Directory.EnumerateFiles(dir))
{
var processId = Path.GetFileNameWithoutExtension(file);
if (thisProcess.Id == int.Parse(processId))
continue;
try
{
File.Delete(file);
continue;
}
catch { }
using var reader = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete));
var args = await reader.ReadToEndAsync().ConfigureAwait(false);
if (args.StartsWith('"'))
args = args.Substring(args.IndexOf('"', 1) + 1);
else
args = args.Split(' ', 2).Last();
WriteLine($"{processId,10} {rt,-3} {args.Trim()}");
}
}
}
}
}