bat 命令 判断如果该exe程序已运行则马上关闭并且重新运行该软件 执行一次 求代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bat 命令 判断如果该exe程序已运行则马上关闭并且重新运行该软件 执行一次 求代码相关的知识,希望对你有一定的参考价值。
bat 命令 判断如果该A.exe程序已运行则马上关闭A.exe并且重新运行该软件A.exe 执行一次 求代码
@echo offset /p a=请输入进程(如QQ.exe):
tasklist | findstr "%a%"||(echo 进程未启动&pause>nul&exit)
taskkill /im "%a%"
wmic process where name="%a%" get executablepath>$
for /f "tokens=*" %%i in ('more +1 $') do start "" %%i
del $
pause
可根据进程自动找到文件存放位置并启动,文件已上传
打开记事本
tasklist可以查询运行程序名
命令行输入第一句,回车看效果,记事本被结束。
命令行直接输入
for /f "tokens=1 delims= " %i in ('tasklist') do (if "%i"=="notepad.exe" TASKKILL /F /IM notepad.exe&&start notepad.exe)
批处理中%i要改为%%i了
for /f "tokens=1 delims= " %%i in ('tasklist') do (if "%%i"=="notepad.exe" TASKKILL /F /IM notepad.exe&&start notepad.exe)
[Winform]只允许运行一个exe,如果已运行则将窗口置前
摘要
接着介绍项目中用到的一些方法,在winform中,打好包,有时并不允许运行多个客户端,要保证只有一个客户端运行。如果已经运行了,则弹出已运行的窗口,使其展示。
方法
判断是否有相同的进程
/// <summary> /// 获取当前是否具有相同进程。 /// </summary> /// <returns></returns> public static Process GetRunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) //确保例程从EXE文件运行 if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) return process; } return null; }
在Main函数中,进行判断
/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { try { Process instance = GetRunningInstance(); Application.SetCompatibleTextRenderingDefault(false); if (instance == null) { Run(); } else { ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOACTIVATE); //调用api函数,正常显示窗口 SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端 } } catch (Exception ex) { LogInfoData.WriteLog(new LogInfo { Message = ex.Message, Op = "start_app_err" }); MessageBox.Show("系统启动出现异常,请重新启动"); } }
windows API
private const int SW_HIDE = 0; private const int SW_NORMAL = 1; private const int SW_MAXIMIZE = 3; private const int SW_SHOWNOACTIVATE = 4; private const int SW_SHOW = 5; private const int SW_MINIMIZE = 6; private const int SW_RESTORE = 9; private const int SW_SHOWDEFAULT = 10; [DllImport("User32.dll")] private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(System.IntPtr hWnd);
在使用SetForegroundWindow需要注意以下几点:
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.
A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindowfunction. The process specified by dwProcessId loses the ability to set the foreground window the next time the user generates input, unless the input is directed at that process, or the next time a process calls AllowSetForegroundWindow, unless that process is specified.
The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function.
地址:https://msdn.microsoft.com/en-us/library/ms633539(v=VS.85).aspx
这个过程是前台进程。
这个过程是由前台进程的开始。
过程中收到的最后一个输入事件。
没有前台进程。
前台进程正在被调试。
前景不被锁定(见LockSetForegroundWindow)。
前景锁定超时已过期(看到SPI_GETFOREGROUNDLOCKTIMEOUTSystemParametersInfo)
需要满足上面一条即可,比如如果这个进程是后台进程,则不会起作用。可以在windows任务管理器中,查看是否在窗口最小化的时候,变成了后台进程。比如,常用的Hide方法,会是窗口变为后台进程,而窗口最下话操作,则不会改变进程状态,扔是前台进程。
this.WindowState = FormWindowState.Minimized;
以上是关于bat 命令 判断如果该exe程序已运行则马上关闭并且重新运行该软件 执行一次 求代码的主要内容,如果未能解决你的问题,请参考以下文章