如何通过主机名C#检查映射的网络驱动器是否存在
本文关键字:网络 驱动器 是否 存在 映射 检查 何通过 主机 | 更新日期: 2023-09-27 18:01:12
我的代码C#中有一个问题。我无法通过主机名检查映射的网络驱动器是否存在。我可以通过带有ping功能的ip地址进行检查。但实际问题不在于ip地址。我需要检查主机名。
Ping ping = new Ping();
var reply = ping.Send("ADS-201");
if (reply.Status == IPStatus.Success) {
NetworkDrive oNetDrive = new NetworkDrive();
oNetDrive.LocalDrive = "Z:";
oNetDrive.ShareName = "''''ADS-201''fileserver''public";
oNetDrive.MapDrive();
}
这是一个答案:我的朋友想好了,我只是在分享谁需要它。
var searcher = new ManagementObjectSearcher(
"root''CIMV2",
"SELECT * FROM Win32_MappedLogicalDisk");
List<string> gunler = new List<string>();
try
{
while (true)
{
Thread.Sleep(60 * 1 * 100);
gunler.Clear();
foreach (ManagementObject queryObj in searcher.Get())
{
gunler.Add(queryObj["ProviderName"].ToString().Substring(2,7));
//MessageBox.Show("Caption: " + queryObj["ProviderName"] + " ---" + queryObj["FileSystem"]);
}
// Console.WriteLine("*** calling MyMethod *** ");
IPAddress ip = IPAddress.Parse("192.168.1.123");
//IPAddress hostn = IPAddress.Parse("ADS-201");
Ping ping = new Ping();
var reply = ping.Send(ip);
// var hosreply = ping.Send(hostn);
if (reply.Status == IPStatus.Success)
{
if (gunler.Contains("ads-201") || gunler.Contains("ADS-201"))
{
MessageBox.Show("ADS-201 is exist");
}
else
{
MessageBox.Show("ADS-201 does not exist!");
}
}
else
{
MessageBox.Show("No ping to hostname!");
}
// yuor_method();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}