Check if App is running on network PC
I have a C# app application working to some extent. What i need to do is to continue execution if a computer (given an IP Address) is running an application (TEKBSS.exe). How can i do that? Can someone help m开发者_JAVA技巧e?
You can do this through WMI. You'll need appropriate credentials to access the remote machine.
The System.Management namespace includes features for using WMI from C#.
Here you go:
// 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;
The scope tells WMI what machine to execute the query on, so don't forget to change the IP address accordingly.
The ManagementObjectSearcher will then query the machine for a list of all processes with the name TEKBSS.exe.
You can use WMI to query information on remote machines, such as which programs are running.
You will need to reference System.Management.dll
, and have appropriate rights on the remote machine to access 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);
}
}
Look into:
System.Diagnostics.Process
Process p = new Process();
p.StartInfo.FileName = "TEKBSS.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
EDIT:
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);
}
}
I know this is .net code. But I used this a while ago to do the same. Hopefully it will give you an idea, and I will try and convert. As long as you have permission rights you can execute the command pushd
in your code. You can try executing from the command line first to make sure you can get in.
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);
You will most likely want to execute popd
after to return to the directory on your machine.
Let me see if I can convert to C for you. I will edit in a bit.
EDIT
Link to executing commands in C#
精彩评论