净DriveInfo()与UNC路径?路径、DriveInfo、UNC

2023-09-03 01:41:14 作者:打小就傲

早上好,

有没有办法来获取UNC路径DriveInfo实例(例如\ fors343a.ww123.somedomain.net \文件夹\ 1 \),因为例如...

  VAR driveInfo =新System.IO.DriveInfo(驱动器);
 

使用UNC路径以上时

我会怎么才能检索有关或如信息使用我将如何检查一个给定的文件夹是否位于本地驱动器或UNC路径?

解决方案

DriveInfo 构造函数的备注部分说:

  

该驱动器名称必须是一个   从大写或小写字母'A'   以Z。您无法使用此方法   获得驱动器名称的信息   是nullNothingnullptrnull引用   (在Visual Basic中为Nothing),或者使用UNC   (\服务器\共享)路径。

我是能够使其通过映射在Windows资源管理器中的网络驱动器的工作。也就是说,我映射\服务器\共享开车Z,然后 DriveInfo(Z:\\); 给我我所期待的。

不幸的是,有没有简单的方法映射从C#网络驱动器。你要么必须执行一个外部命令(即净使用Z:\服务器\共享),或调用Windows的 WNetAddConnection2 API函数来做到这一点。无论哪种方式你去,你将需要删除驱动器映射时,你就大功告成了。

Good morning,

欧盟一类医疗器械CE认证需要技术文件吗

is there a way to get a DriveInfo instance for UNC paths (e.g. "\fors343a.ww123.somedomain.net\folder\1\") because for example...

var driveInfo = new System.IO.DriveInfo(drive);

... throws an ArgumentException ("Object must be a root directory (\"C:\\") or a drive letter (\"C\").") when using that UNC path above.

What would I use in order to retrieve information about that or e.g. how would I check whether a given folder resides on a local drive or an unc path?

解决方案

The Remarks section for the DriveInfo constructor says:

The drive name must be either an uppercase or lowercase letter from 'a' to 'z'. You cannot use this method to obtain information on drive names that are nullNothingnullptra null reference (Nothing in Visual Basic) or use UNC (\server\share) paths.

I was able to make it work by mapping a network drive in Windows Explorer. That is, I mapped "\server\share" to drive Z, and then DriveInfo("Z:\\"); gave me what I expected.

Unfortunately, there's no simple way to map a network drive from C#. You'll either have to execute an external command (i.e. "net use z: \server\share"), or call the Windows WNetAddConnection2 API function to do it. Either way you go, you'll need to remove the drive mapping when you're done.