mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
source code checkin
This commit is contained in:
parent
5f8fb2c825
commit
7e57b72e35
675 changed files with 168433 additions and 0 deletions
20
src/ServerManager.Updater/Utils/IOUtils.cs
Normal file
20
src/ServerManager.Updater/Utils/IOUtils.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ServerManagerTool.Updater
|
||||
{
|
||||
public static class IOUtils
|
||||
{
|
||||
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool DeleteFile(string name);
|
||||
|
||||
public static bool Unblock(string fileName)
|
||||
{
|
||||
return DeleteFile(fileName + ":Zone.Identifier");
|
||||
}
|
||||
|
||||
public static string NormalizePath(string path) => Path.GetFullPath(new Uri(path).LocalPath).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
113
src/ServerManager.Updater/Utils/ProcessUtils.cs
Normal file
113
src/ServerManager.Updater/Utils/ProcessUtils.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace ServerManagerTool.Updater
|
||||
{
|
||||
public static class ProcessUtils
|
||||
{
|
||||
public static string FIELD_COMMANDLINE = "CommandLine";
|
||||
public static string FIELD_EXECUTABLEPATH = "ExecutablePath";
|
||||
public static string FIELD_PROCESSID = "ProcessId";
|
||||
|
||||
private static Mutex _mutex;
|
||||
|
||||
[DllImport("shell32.dll", SetLastError = true)]
|
||||
static extern IntPtr CommandLineToArgvW([MarshalAs(UnmanagedType.LPWStr)] string lpCmdLine, out int pNumArgs);
|
||||
|
||||
public static string[] CommandLineToArgs(string commandLine)
|
||||
{
|
||||
var argv = CommandLineToArgvW(commandLine, out int argc);
|
||||
if (argv == IntPtr.Zero)
|
||||
throw new Win32Exception();
|
||||
|
||||
try
|
||||
{
|
||||
var args = new string[argc];
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
var p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
|
||||
args[i] = Marshal.PtrToStringUni(p);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeHGlobal(argv);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetCommandLineForProcess(int processId)
|
||||
{
|
||||
var wmiQueryString = $"SELECT {FIELD_COMMANDLINE} FROM Win32_Process WHERE {FIELD_PROCESSID} = {processId}";
|
||||
|
||||
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
|
||||
{
|
||||
using (var results = searcher.Get())
|
||||
{
|
||||
ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
|
||||
if (mo != null)
|
||||
return (string)mo[FIELD_COMMANDLINE];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetMainModuleFilepath(int processId)
|
||||
{
|
||||
var wmiQueryString = $"SELECT {FIELD_EXECUTABLEPATH} FROM Win32_Process WHERE {FIELD_PROCESSID} = {processId}";
|
||||
|
||||
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
|
||||
{
|
||||
using (var results = searcher.Get())
|
||||
{
|
||||
ManagementObject mo = results.Cast<ManagementObject>().FirstOrDefault();
|
||||
if (mo != null)
|
||||
return (string)mo[FIELD_EXECUTABLEPATH];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Process GetProcess(int processId)
|
||||
{
|
||||
return Process.GetProcessById(processId);
|
||||
}
|
||||
|
||||
public static Process[] GetProcesses(string processName, string executablePath)
|
||||
{
|
||||
var runningProcesses = Process.GetProcessesByName(processName).ToList();
|
||||
|
||||
for (var i = runningProcesses.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var process = runningProcesses[i];
|
||||
var runningPath = GetMainModuleFilepath(process.Id);
|
||||
if (!string.Equals(executablePath, runningPath, StringComparison.OrdinalIgnoreCase))
|
||||
runningProcesses.RemoveAt(i);
|
||||
}
|
||||
|
||||
return runningProcesses.ToArray();
|
||||
}
|
||||
|
||||
public static bool IsAlreadyRunning()
|
||||
{
|
||||
var assemblyLocation = Assembly.GetEntryAssembly().Location;
|
||||
var name = $"Global::{Path.GetFileName(assemblyLocation)}";
|
||||
|
||||
_mutex = new Mutex(true, name, out bool createdNew);
|
||||
if (createdNew)
|
||||
_mutex.ReleaseMutex();
|
||||
|
||||
return !createdNew;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
src/ServerManager.Updater/Utils/ScriptUtils.cs
Normal file
15
src/ServerManager.Updater/Utils/ScriptUtils.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
namespace ServerManagerTool.Updater
|
||||
{
|
||||
public static class ScriptUtils
|
||||
{
|
||||
public static string AsQuoted(this string parameter)
|
||||
{
|
||||
var newValue = parameter;
|
||||
if (!newValue.StartsWith("\""))
|
||||
newValue = "\"" + newValue;
|
||||
if (!newValue.EndsWith("\""))
|
||||
newValue = newValue + "\"";
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue