|
|
@ -116,10 +116,13 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 运行WebApi发布程序
|
|
|
|
/// 新窗口运行WebApi发布程序
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="exeFile">WebApi程序文件</param>
|
|
|
|
|
|
|
|
/// <param name="args">WebApi启动参数</param>
|
|
|
|
|
|
|
|
/// <param name="stayWindow">是否保持命令窗口(不关闭)</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string RunWebApiExeFile(string exeFile, bool stayWindows = false, params string[] args)
|
|
|
|
public static string RunWebApiExeFile(string exeFile, bool stayWindow = false, params string[] args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
string executedMessage = string.Empty;
|
|
|
|
string executedMessage = string.Empty;
|
|
|
|
try
|
|
|
|
try
|
|
|
@ -135,29 +138,39 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
return executedMessage;
|
|
|
|
return executedMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//纯文件名(不带扩展名)作用 新窗口标题
|
|
|
|
string fileName = Path.GetFileNameWithoutExtension(exeFile);
|
|
|
|
string fileName = Path.GetFileNameWithoutExtension(exeFile);
|
|
|
|
|
|
|
|
|
|
|
|
string stayWindowsArg = stayWindows ? "/k" : "/c";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//不同操作系统,需要不同的启动命令
|
|
|
|
string systemShell = "cmd.exe";
|
|
|
|
string systemShell = "cmd.exe";
|
|
|
|
|
|
|
|
|
|
|
|
//string webApiArgs = $"{stayWindowsArg} start \"{fileName}\" \"{exeFile}\" {string.Join(" ", args)}";
|
|
|
|
string cmdArgsText = string.Empty;
|
|
|
|
//或者
|
|
|
|
|
|
|
|
string webApiArgs = $"{stayWindowsArg} start \"{fileName}\" \"\"\"{exeFile}\" {string.Join(" ", args)}";
|
|
|
|
//保持新命令窗口(不自动关闭)
|
|
|
|
|
|
|
|
if (stayWindow)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
cmdArgsText = $"/c start \"{fileName}\" cmd /k \"\"\"{exeFile}\" {string.Join(" ",args)}";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
cmdArgsText = $"/c start \"{fileName}\" \"\"\"{exeFile}\" {string.Join(" ",args)}";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
FileName = systemShell,
|
|
|
|
FileName = systemShell,
|
|
|
|
Arguments = webApiArgs,
|
|
|
|
Arguments = cmdArgsText,
|
|
|
|
|
|
|
|
|
|
|
|
//未知原因:只有 UseShellExecute 设置为 true 时,CreateNoWindow参数才有效,新窗口执行才实际有效。
|
|
|
|
//执行命令的线程本身不弹出新窗口
|
|
|
|
UseShellExecute = true,
|
|
|
|
//确保弹出新窗口:是使用cmd start 实现
|
|
|
|
|
|
|
|
//保持新窗口不关闭:使用 start cmd /k 实现
|
|
|
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
|
|
|
|
|
|
|
//true时不创建新窗口,false才是创建新窗口
|
|
|
|
//true时不创建新窗口,false才是创建新窗口
|
|
|
|
CreateNoWindow = false,
|
|
|
|
CreateNoWindow = false,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//启动进程
|
|
|
|
//启动进程
|
|
|
|
using (Process process = new Process() { StartInfo = startInfo })
|
|
|
|
using (Process process = new Process() { StartInfo = startInfo })
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -209,40 +222,6 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
return executedMessage;
|
|
|
|
return executedMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 互斥锁是否存在
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="mutexName">互斥锁名称</param>
|
|
|
|
|
|
|
|
/// <returns>是否存在</returns>
|
|
|
|
|
|
|
|
public static bool WebAppIsRunningByMutex(string mutexName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool createdResult = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建互斥锁
|
|
|
|
|
|
|
|
using (var mutex = new Mutex(true, mutexName, out createdResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (createdResult)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//互斥锁是否创建成功
|
|
|
|
|
|
|
|
return !createdResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 进程是否存在
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="processName">进程名</param>
|
|
|
|
|
|
|
|
/// <returns>是否存在</returns>
|
|
|
|
|
|
|
|
public static bool WebAppIsRunningByProcessName(string processName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool processExists = Process.GetProcessesByName(processName).ToList().Count == 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return processExists;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 执行CMD命令
|
|
|
|
/// 执行CMD命令
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
@ -292,5 +271,39 @@ namespace HttpClientStudy.Core.Utilities
|
|
|
|
return executedMessage;
|
|
|
|
return executedMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 互斥锁是否存在
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="mutexName">互斥锁名称</param>
|
|
|
|
|
|
|
|
/// <returns>是否存在</returns>
|
|
|
|
|
|
|
|
private static bool WebAppIsRunningByMutex(string mutexName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool createdResult = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建互斥锁
|
|
|
|
|
|
|
|
using (var mutex = new Mutex(true, mutexName, out createdResult))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (createdResult)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mutex.ReleaseMutex();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//互斥锁是否创建成功
|
|
|
|
|
|
|
|
return !createdResult;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 进程是否存在
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="processName">进程名</param>
|
|
|
|
|
|
|
|
/// <returns>是否存在</returns>
|
|
|
|
|
|
|
|
private static bool WebAppIsRunningByProcessName(string processName)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
bool processExists = Process.GetProcessesByName(processName).ToList().Count == 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return processExists;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|