如何通过主机名C#检查映射的网络驱动器

时间:2016-08-08 07:15:34

标签: c# mapped-drive

我的代码C#中存在问题。如果按主机名存在,我无法检查映射的网络驱动器。我可以通过ip地址检查ping功能。但实际问题不是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(); 
} 

1 个答案:

答案 0 :(得分:0)

这是一个答案:我的朋友弄明白,我只是分享谁需要它。

 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);
           }