[英]How to ping a mapped drive
Hello I'm writing an application where the user can enter a directory for storing some files.您好,我正在编写一个应用程序,用户可以在其中输入用于存储某些文件的目录。
In case he entered a directory on the mashine the program is running on, I can just use the path he entered.如果他输入了运行程序的机器上的目录,我可以使用他输入的路径。
In case he entered a directory on the network like this: 192.168.xxx.xxx..., I can ping and if pingable use this path.如果他在网络上输入这样的目录:192.168.xxx.xxx...,我可以 ping 并且如果可以 ping 使用此路径。
But what if the user has a mapped drive and enteres something like Y:\\work... ?但是如果用户有一个映射驱动器并输入类似 Y:\\work... 的内容怎么办? The .Net Ping class has a Send method, I'm using it like this:
.Net Ping 类有一个 Send 方法,我是这样使用它的:
try{
Ping pinger = new Ping();
PingReply reply = pinger.Send(NetworkIP);
} catch (PingException) {
// Discard PingExceptions and return false;
}
But if the NetworkIP is Y:\\ or something it throws a PingException even if the drive should be available.但是,如果 NetworkIP 是 Y:\\ 或其他内容,即使驱动器应该可用,它也会抛出 PingException。
Does anybody know how I can fix this?有谁知道我该如何解决这个问题? Maybe I can get the Ip address of the drive somehow and use it instead?
也许我可以以某种方式获取驱动器的 IP 地址并使用它?
thanks for you help谢谢你的帮助
You could just check if that network drive is ready.您可以检查该网络驱动器是否准备就绪。
There's a DriveInfo class in the .NET Framework which indicates if a drive is ready (if a CD is inserted in a CD ROM device or if a removable storage is ready for IO, which also applies to network drives). .NET Framework 中有一个DriveInfo类,它指示驱动器是否准备就绪(如果 CD 插入到 CD ROM 设备中,或者可移动存储是否已准备好进行 IO,这也适用于网络驱动器)。
I've quickly tested this with a small PowerShell script to verify this and this is my output:我已经用一个小的 PowerShell 脚本快速测试了这一点来验证这一点,这是我的输出:
Name : Y:\
DriveType : Network
DriveFormat :
IsReady : False
AvailableFreeSpace :
TotalFreeSpace :
TotalSize :
RootDirectory : Y:\
VolumeLabel :
Name : Z:\
DriveType : Network
DriveFormat : HGFS
IsReady : True
AvailableFreeSpace : 420309352448
TotalFreeSpace : 420309352448
TotalSize : 500105736192
RootDirectory : Z:\
VolumeLabel : Shared Folders
You can use the example given in the MSDN documentation to write the code for your application which could look like this (didn't test yet but should work):您可以使用 MSDN 文档中给出的示例为您的应用程序编写代码,它可能如下所示(尚未测试但应该可以工作):
using System;
using System.IO;
class MyClass {
public bool CheckNetworkDrive(String name) {
bool result = false;
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo d in drives) {
if (d.Name == name) {
result = d.IsReady;
}
}
return result;
}
}
Also you can use net use to find IP or Computer Name of mapped network drive您也可以使用net use查找映射网络驱动器的 IP 或计算机名称
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c net use";
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.Contains("U:")).FirstOrDefault();
if (!string.IsNullOrEmpty(line))
{
var host = line.Substring(line.IndexOf("\\"), line.Substring(line.IndexOf("\\")).IndexOf(" ")).Split(new[]{'\\'}, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
Ping pinger = new Ping();
PingReply reply = pinger.Send(host);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.