使用C#.net自动检测局域网上的应用程序
我有一个开源的语音聊天应用程序,在局域网上运行良好 但问题在于手动连接两台电脑 假设有两台PC(应用程序实例),PC A和PC B。 要建立连接,我必须将PC a的IP地址放入PC B,将PC B的IP地址放入PC a。 我想做一个小的代码更改,如果应用程序运行在两台PC上,并且它们都通过LAN连接,那么双方都会自动获得IP地址。比如自动检测。 因此,我的逻辑是首先使用使用C#.net自动检测局域网上的应用程序,c#,.net,ip-address,connect,lan,C#,.net,Ip Address,Connect,Lan,我有一个开源的语音聊天应用程序,在局域网上运行良好 但问题在于手动连接两台电脑 假设有两台PC(应用程序实例),PC A和PC B。 要建立连接,我必须将PC a的IP地址放入PC B,将PC B的IP地址放入PC a。 我想做一个小的代码更改,如果应用程序运行在两台PC上,并且它们都通过LAN连接,那么双方都会自动获得IP地址。比如自动检测。 因此,我的逻辑是首先使用arp-a命令知道局域网上的IP地址,然后将输出写入文本文件,只获取以192开头的IP地址。。。并使用检查每个地址上的应用程序实
arp-a
命令知道局域网上的IP地址,然后将输出写入文本文件,只获取以192开头的IP地址。。。并使用检查每个地址上的应用程序实例。
不幸的是,我最终得到一个错误,它表明
未处理的异常:System.Runtime.InteropServices.ComeException:
RPC服务器不可用
在
System.Runtime.InteropServices.Marshal.ThroweExceptionForhr内部(Int32
错误代码,IntPtr errorInfo)位于
System.Management.ManagementScope.InitializeGuts(对象o)位于
System.Management.ManagementScope.Initialize()位于
System.Management.ManagementObjectSearcher.Initialize()位于
System.Management.ManagementObjectSearcher.Get()
这是我用来完成这项任务的代码
//Write the cmd Output in a text file
string strCmdText;
strCmdText = @"/K arp -a > C:\Test\Result.txt";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
//To get the IP address which starts with the 192.
const string ipPattern = @"^\s*(192\.168\.\d{1,3}\.\d{1,3}\b)";
var ipRegex = new Regex(ipPattern);
var ipAddresses192168 = File.ReadAllLines(@"C:\Test\Result.txt")
.Skip(3) // Skip 3 lines
.Where(line => ipRegex.IsMatch(line))
.Select(line => ipRegex.Match(line).Groups[1].Value);
foreach (var ipAddress in ipAddresses192168)
{
Console.WriteLine(ipAddress);
// Check for the running instance of the application on LAN network.
ManagementScope scope = new ManagementScope(@"\\" + ipAddress + @"\root\cimv2");
string query = "SELECT * FROM Win32_Process WHERE Name='WavPlayer.exe'";
var searcher = new ManagementObjectSearcher(query);
searcher.Scope = scope;
bool isRunning = searcher.Get().Count > 0;
}
所以我的问题是,是否有其他直接的过程来使用C#.net完成这项任务
我可以提供更多关于这个问题的信息,所以任何帮助都是感激的