체크인 경우 특정 exe 파일을 실행하
-
20-09-2019 - |
문제
나는 방법을 알고 싶다면 확인할 수 있습 프로그램 특정 위치에 실행되는 경우.예를 들어가는 두 가지 위치 test.exe 에 c:\loc1 est.exe 고 c:\loc2 est.exe.나는 단지 알고 싶어하는 경우 c:\loc1 est.exe 실행하지 않는 모든의 인스턴스 test.exe.
해결책
bool isRunning = Process.GetProcessesByName("test")
.FirstOrDefault(p => p.MainModule.FileName.StartsWith(@"c:\loc1")) != default(Process);
다른 팁
이것은 나의 개선 된 기능입니다.
private bool ProgramIsRunning(string FullPath)
{
string FilePath = Path.GetDirectoryName(FullPath);
string FileName = Path.GetFileNameWithoutExtension(FullPath).ToLower();
bool isRunning = false;
Process[] pList = Process.GetProcessesByName(FileName);
foreach (Process p in pList) {
if (p.MainModule.FileName.StartsWith(FilePath, StringComparison.InvariantCultureIgnoreCase))
{
isRunning = true;
break;
}
}
return isRunning;
}
다음과 같이 사용하십시오.
ProgramIsRunning(@"c:\loc1\test.exe");
이것을 시도해보십시오 ... 다른 프로세스가 이미 시작하려고하는 EXE와 같은 이름으로 다른 프로세스를 실행중인 경우 시작시 결정을 결정한 다음 이미 전경으로 가져 오십시오. 실행 중 ... 프로세스 이름을 가져 와서 해당 특정 이름을 테스트하도록 수정할 수 있습니다 ... 특정 이름으로 실행되는 프로세스가 있지만 해당 프로세스가로드 된 위치는 ...
지정된 이름으로 실행되는 프로세스가있는 경우 해당 프로세스가로드 된 위치를 반환하는 노출 된 액세스 방법이있는 경우 실행중인 프로세스에서 해당 메소드를 호출 할 수 있습니다. 그렇지 않으면 모르겠습니다.
그러나 호기심이 없어도 왜 다르지 않으면 왜 신경 쓰나요? 그리고 어떤 식 으로든 다르면, 그 차이를 사용하여 (무엇이든)로드되는 코드는로드 된 것을 감지합니다. 그러나 그들이 동일하다면 어떤 온 디스크 이미지를로드하는 데 사용되었는지는 어떻게 중요 할 수 있습니까?
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
private static bool IsAlreadyRunning()
{
// get all processes by Current Process name
Process[] processes =
Process.GetProcessesByName(
Process.GetCurrentProcess().ProcessName);
// if there is more than one process...
if (processes.Length > 1)
{
// if other process id is OUR process ID...
// then the other process is at index 1
// otherwise other process is at index 0
int n = (processes[0].Id == Process.GetCurrentProcess().Id) ? 1 : 0;
// get the window handle
IntPtr hWnd = processes[n].MainWindowHandle;
// if iconic, we need to restore the window
if (IsIconic(hWnd)) ShowWindowAsync(hWnd, SW_RESTORE);
// Bring it to the foreground
SetForegroundWindow(hWnd);
return true;
}
return false;
}
모든 기존 프로세스를 반복 한 다음 원하는 파일 이름에 대해 MainModule 속성을 확인해야합니다. 이 같은
using System.Diagnostics;
using System.IO;
//...
string fileNameToFilter = Path.GetFullPath("c:\\loc1\\test.exe");
foreach (Process p in Process.GetProcesses())
{
string fileName = Path.GetFullPath(p.MainModule.FileName);
//cehck for equality (case insensitive)
if (string.Compare(fileNameToFilter, fileName, true) == 0)
{
//matching...
}
}
이 기능은 도움이 될 수 있습니다.
using System.Diagnostics;
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses()) {
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
이 같은. getMainMainModuleFileName은 X86에서 X64 프로세스에 액세스하는 데 도움이됩니다.
[DllImport("kernel32.dll")]
public static extern bool QueryFullProcessImageName(IntPtr hprocess, int dwFlags, StringBuilder lpExeName, out int size);
private bool CheckRunningProcess(string processName, string path) {
Process[] processes = Process.GetProcessesByName(processName);
foreach(Process p in processes) {
var name = GetMainModuleFileName(p);
if (name == null)
continue;
if (string.Equals(name, path, StringComparison.InvariantCultureIgnoreCase)) {
return true;
}
}
return false;
}
// Get x64 process module name from x86 process
private static string GetMainModuleFileName(Process process, int buffer = 1024) {
var fileNameBuilder = new StringBuilder(buffer);
int bufferLength = fileNameBuilder.Capacity + 1;
return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, out bufferLength) ?
fileNameBuilder.ToString() :
null;
}
당신은 a를 사용할 수 있습니다 MUTEX라는 이름, 그것은 프로그램이 실행중인 디렉토리 구조에서 명명되었습니다.
System.Reflection.Assembly.GetEntryAssembly()
이것을 가져올 것이 당신을 위해 많은 정보에 대한 항목과 같은 어셈블리:
System.Reflection.Assembly.GetEntryAssembly().CodeBase;
이 말할 것이의 위치를 실행하는 어셈블리입니다.