我怎么知道哪个openoffice应用程序正在运行?

我想查看进程列表以确定OpenOffice Calc是否正在运行,或者OpenOffice Writer是否正在运行。 随着QuickStartclosures,我得到一个scalc.exe和一个swriter.exe,所以它很简单。

但是,快速启动时,我只是得到soffice.bin和soffice.exe

有没有办法来问这些进程哪个应用程序正在运行?

我认为没有办法检查窗口标题。 你将不得不枚举所有窗口,并检查标题是否以“ – OpenOffice.org Writer”或“ – OpenOffice.org Calc”等结尾

以下简短的示例将检查Writer是否正在运行:

using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace Sample { public class Window { public string Title { get; set; } public int Handle { get; set; } public string ProcessName { get; set; } } public class WindowHelper { /// <summary> /// Win32 API Imports /// </summary> [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size); [DllImport("user32.dll")] private static extern int EnumWindows(EnumWindowsProc ewp, int lParam); [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam); private List<Window> _openOfficeWindows; public List<Window> GetOpenOfficeWindows() { _openOfficeWindows = new List<Window>(); EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow); EnumWindows(ewp, 0); return _openOfficeWindows; } private bool EvalWindow(IntPtr hWnd, int lParam) { if (!IsWindowVisible(hWnd)) return (true); uint lpdwProcessId; StringBuilder title = new StringBuilder(256); GetWindowThreadProcessId(hWnd, out lpdwProcessId); GetWindowText(hWnd, title, 256); Process p = Process.GetProcessById((int)lpdwProcessId); if (p != null && p.ProcessName.ToLower().Contains("soffice")) { _openOfficeWindows.Add(new Window() { Title = title.ToString(), Handle = hWnd.ToInt32(), ProcessName = p.ProcessName }); } return (true); } } class Program { static void Main(string[] args) { WindowHelper helper = new WindowHelper(); List<Window> openOfficeWindows = helper.GetOpenOfficeWindows(); foreach (var item in openOfficeWindows) { if (item.Title.EndsWith("- OpenOffice.org Writer")) { Console.WriteLine("Writer is running"); } } } } }