特定のexeファイルが実行されているかどうかを確認する
-
20-09-2019 - |
質問
特定の場所でプログラムが実行されているかどうかを確認する方法を知りたいです。たとえば、test.exe には c:\loc1 est.exe と c:\loc2 est.exe の 2 つの場所があります。私が知りたかったのは、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");
私は、起動時に決定するためにそれを使用する...これを試してみて、そしてちょうど、前面に1つを持って(と集中します)それは、特定の名前で実行中のプロセスがある場合、これはあなたを教えてくれます...すでに実行している...あなたがその特定の名前のためのプロセス名とテストを取るために、それを修正することができますが、ないそのプロセスはからロードされた場所..ます。
指定した名前の実行中のプロセスがある場合、そのプロセスは、それがからロードされた場所を返す露出アクセス方法を持っていた場合は、、そして、あなたがそうでない場合、私は知らない、実行中のプロセスにそのメソッドを呼び出すことができます。..
しかし、彼らは異なっている場合を除き、単に好奇心のうち、なぜあなたは、気にしていますか?そして、彼らはいくつかの方法で異なるなら、コードが読み込まれているかを検出するために(それが何であれ)その違いを使用します。しかし、彼らが同じなら、どのようにディスク上のイメージは、それをロードするために使用された問題ではできますか?
[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;
}
このようなもの。GetMainModuleFileName は、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;
}
あなたはという名前のミューテックスを使用することができますする 、それはプログラムが実行されているディレクトリ構造のオフ名付けいます。
System.Reflection.Assembly.GetEntryAssembly()
これはのような、あなたのためのエントリー・アセンブリに関する情報の多くをもたらすでしょう
System.Reflection.Assembly.GetEntryAssembly().CodeBase;
これは、実行中のアセンブリの場所を教えてくれます。