我有一个C#app应用程序在某种程度上工作。我需要做的是,如果计算机(给定IP地址)正在运行应用程序(TEKBSS.exe),则继续执行。我怎样才能做到这一点?有人能帮助我吗?
答案 0 :(得分:6)
您可以通过WMI执行此操作。您需要适当的凭据才能访问远程计算机。
System.Management命名空间包含使用C#中的WMI的功能。
你走了:
// Don't forget...
// using System.Management; <-- Need to add a reference to System.Management, too.
ManagementScope scope = new ManagementScope(@"\\192.168.1.73\root\cimv2");
string query = "SELECT * FROM Win32_Process WHERE Name='TEKBSS.exe'";
var searcher = new ManagementObjectSearcher(query);
searcher.Scope = scope;
bool isRunning = searcher.Get().Count > 0;
范围告诉WMI执行查询的机器,所以不要忘记相应地更改IP地址。
然后,ManagementObjectSearcher将在机器上查询名为TEKBSS.exe的所有进程的列表。
答案 1 :(得分:3)
您可以使用WMI查询远程计算机上的信息,例如正在运行的程序。
您需要引用System.Management.dll
,并在远程计算机上拥有访问WMI的相应权限。
using System;
using System.Linq;
using System.Management;
namespace Bling
{
public static void Main()
{
const string Host = "vmhost01";
const string Path = (@"\\" + Host + @"\root\CIMV2");
const string Exe = "TEKBSS.exe";
var queryString = string.Format("SELECT Name FROM Win32_Process WHERE Name = '{0}'", Exe);
var query = new SelectQuery(queryString);
var options = new ConnectionOptions();
options.Username = "Administrator";
options.Password = "*";
var scope = new ManagementScope(Path, options);
var searcher = new ManagementObjectSearcher(scope, query);
bool isRunnning = searcher.Get().Count > 0;
Console.WriteLine("Is {0} running = {1}.", Exe, isRunnning);
}
}
答案 2 :(得分:0)
我知道这是.net代码。但我前一段时间也用这个做同样的事。希望它会给你一个想法,我会尝试转换。只要您拥有权限,就可以在代码中执行命令pushd
。您可以先尝试从命令行执行,以确保可以进入。
//pushes into the Given Machine on the C:\ and filters for your program
Dim sCommand as String = "pushd \\<MachineName>\C$ && tasklist.exe /FI ""IMAGENAME eq <NameOfExecutable>.exe""
//execute command from program
Shell("cmd.exe /c" & sCommand. AppWinStyle.Hide, True);
您很可能希望在返回计算机上的目录后执行popd
。
让我看看我是否可以为你转换为C.我稍后会编辑。
修改强>
答案 3 :(得分:-1)
查看:
System.Diagnostics.Process
Process p = new Process();
p.StartInfo.FileName = "TEKBSS.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
编辑:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Security;
public class MainClass
{
public static void Main()
{
Process[] allProcs = Process.GetProcesses("RemoteMachineOnYourNetwork");
foreach (Process p in allProcs)
Console.WriteLine(" -> {0} - {1}", p.ProcessName, p.PeakWorkingSet64);
}
}