Files
runas/Runas/bin/Debug/net5.0/Program.cs
2023-05-10 13:38:05 +03:00

69 lines
2.5 KiB
C#

using Newtonsoft.Json;
using System.Diagnostics;
using System;
using System.Text.RegularExpressions;
namespace Runas
{
class Program
{
public static string DataText = RegexAllEnv(Runas.Properties.Resources.progs);
public static dynamic jprograms = JsonConvert.DeserializeObject(DataText);
public static string RegexAllEnv(string data)
{
System.Collections.Hashtable env = (System.Collections.Hashtable)Environment.GetEnvironmentVariables();
string val;
foreach (System.Collections.DictionaryEntry key in env)
{
val = (string)key.Value;
val = val.Replace(@"\", @"\\");
if (key.Key != "Path" && key.Key != "path")
{
data = val.EndsWith(@"\") ? Regex.Replace(data, $"\\u0025{key.Key}\\u0025\\\\", val, RegexOptions.IgnoreCase) : Regex.Replace(data, $"\\u0025{key.Key}\\u0025", val, RegexOptions.IgnoreCase);
// val.EndsWith(@"\")? data.Replace($"%{key.Key}%\\", val) : data.Replace($"%{key.Key}%",val);
}
}
return data;
}
public static bool DoRun(string program)
{
Process[] process = Process.GetProcessesByName(program);
return (process.Length > 0);
}
public static void RunProcess(string programName)
{
if (!(DoRun(programName)))
{
if (programName.Equals("AutoHotKey"))
{
Process.Start(jprograms[programName][0].Value, $"\u0022{jprograms[programName][1].Value.ToString()}\u0022");
}
else if (jprograms[programName].Count > 1)
{
foreach (var prog in jprograms[programName])
{
if (System.IO.File.Exists(prog.Value))
{
Process.Start(prog.Value);
}
}
}
else if (jprograms[programName].Count == 1)
{
Process.Start(jprograms[programName][0].Value);
}
}
}
static void Main(string[] args)
{
foreach (var prog in jprograms)
{
string ProgProg = ((Newtonsoft.Json.Linq.JProperty)prog).Name;
System.Threading.Thread thread = new(new System.Threading.ThreadStart(() => RunProcess(ProgProg)));
thread.Start();
}
}
}
}