Good morning,
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?
On Windows, the following works great in C# (at least to get the sizes, which is what is most commonly needed):
Here is some sample code (this hasn't actually been compiled in this form, but is bits and pieces from working code scattered across multiple files):
Don't try to use this on Linux or Mac--it would have to be rewritten for those (and I'd be interested to see that).
The Remarks section for the
DriveInfo
constructor says: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.