我想知道我可以在特定位置检查程序是否正在运行。例如有一种在C TEST.EXE两个位置:\ LOC1 \ TEST.EXE和c:\ LOC2 \ TEST.EXE。我只是想知道,如果C:\ LOC1 \ test.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;
} 

来源: http://www.dreamincode.net/code/snippet1541.htm

这样的事情。 GetMainModuleFileName有助于从86访问64过程

  [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;

这将告诉运行组件的位置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top