121 lines
4.3 KiB
C#
121 lines
4.3 KiB
C#
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)
|
|
{
|
|
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(className, cspl.iseverity.warning, "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, cspl.iseverity.info);
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
spl?.WriteException(className, module, 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(className, module, 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;
|
|
}
|
|
}
|
|
}
|