eDosStationFull/eDosStation_Backup_2025.02.04_07.47.06/cDevCon.cs

125 lines
4.2 KiB
C#
Raw Normal View History

2026-08-18 12:15:26 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cspl = LSWLib.CLog;
namespace eDosStation
{
public class cDevCon
{
public StringBuilder error = null;
public bool hasError;
public bool hasCommand;
public string DevConFolder, DevConExePath;
public string comdefault;
cspl spl;
System.Diagnostics.Process DevConProcess = null;
const string usbIdentifier = @"USB\VID_0EBB&PID_0340&MI_00";
const string cDevConFolder = "extra";
const string cDevConexe = "devcon.exe";
const string className = "cDevCon";
public static bool IsAdministrator()
{
return (new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()))
.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
}
public cDevCon(int iCOM, cspl _spl)
{
const string module = "cDevCon";
spl = _spl;
comdefault = "COM" + iCOM.ToString();
hasCommand = false;
DevConProcess = null;
error = new StringBuilder();
hasError = false;
if (IsAdministrator())
{
DevConFolder = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, cDevConFolder);
if (System.IO.Directory.Exists(DevConFolder))
{
DevConExePath = System.IO.Path.Combine(DevConFolder, cDevConexe);
if (System.IO.File.Exists(DevConExePath)) hasCommand = true;
}
}
else
spl?.WriteSplunkLog(module,null, cspl.iseverity.warning, lresultMessage: "No Admin");
}
private string ExecuteDevcon(string command)
{
const string module = "ExecuteDevcon";
if (!hasCommand) return string.Empty;
hasError = false;
error.Clear();
StringBuilder sb = new StringBuilder();
try
{
using (DevConProcess = new System.Diagnostics.Process())
{
DevConProcess.StartInfo.WorkingDirectory = DevConFolder;
DevConProcess.StartInfo.FileName = DevConExePath;
DevConProcess.StartInfo.Arguments = $"{command} \"{usbIdentifier}\"";
DevConProcess.StartInfo.RedirectStandardOutput = true;
DevConProcess.StartInfo.UseShellExecute = false;
DevConProcess.Start();
var i = DevConProcess.StandardOutput;
if (i!=null)
while (!i.EndOfStream)
{
sb.AppendLine(i.ReadLine());
}
DevConProcess.WaitForExit();
spl?.WriteSplunkLog(module, null, cspl.iseverity.info);
}
}
catch (System.Exception ex)
{
spl?.WriteException(module,null, ex);
hasError = true;
sb.Clear();
error.Append(ex.Message);
}
return sb.ToString();
}
public string FindEdosCom()
{
const string module = "FindEdosCom";
string eDosCom = comdefault;
if (hasCommand)
try
{
string cp = ExecuteDevcon("find");
if (!string.IsNullOrEmpty(cp))
{
var ix = cp.IndexOf("(COM");
if (ix >= 0) eDosCom = cp.Substring(ix + 1, 4);
}
}
catch (System.Exception ex)
{
spl?.WriteException(module,null, ex);
}
return eDosCom;
}
public string Restart()
{
string rv = string.Empty;
error.Clear();
hasError = false;
if (hasCommand)
{
rv = ExecuteDevcon("restart");
if (hasError) rv = error.ToString();
}
return rv;
}
}
}