0

I have a problem where users have moved files and data to a NAS. This causes a number of problems. I would like to detect if the network disc users have moved files and data to is a NAS. I want to do this programmatically from a C# program. I want to alert them that this move could cause problems. Is this possible? I've found no way to tell if a network drive is NAS or not from C#/.Net but I haven't found anyone saying it cant be done either. Is there any way to tell if a network drive is a NAS programmatically?


I have revised my question. However, I've had a discussion with the people giving me this issue and I don't belive that the fact that a network disc is a NAS is the problem. Thanks to your comments here I asked a few questions and it turned out that we were talking about different things. The problem would probably be the same no matter what type of network storage that would be used. But just out of curiosity, would it be possible to tell if a network disc is a NAS programmatically?

Ernis
  • 53
  • 1
  • 7
  • 2
    This sounds like an XY problem. Why are you looking for a NAS specifically? Does a SAN count as well? Or any SMB-compatible storage? What about external storage? What is the actual problem? – CodeCaster Jun 16 '17 at 08:50
  • 1
    You mean you want to detect that the selected destination is on a network drive or local disk? If so: https://stackoverflow.com/questions/4396634/how-can-i-determine-if-a-given-drive-letter-is-a-local-mapped-usb-drive – RvdK Jun 16 '17 at 08:51

1 Answers1

3

You need to tie these three things together:

How can I determine if a string is a local folder string or a network string?, Get the drive letter from a path string or FileInfo and How can I determine if a given drive letter is a local/mapped/usb drive?

So:

string path = "\\some-unc-path\file.txt"; // or: @"H:\file2.txt" for mapped network drives

if (new Uri(path).IsUnc)
{
    // network
}
else
{
    var fileInfo = new FileInfo(path);
    var driveLetter = Path.GetPathRoot(fileInfo.FullName);
    var driveInfo = new DriveInfo(driveLetter);
    if (driveInfo.DriveType == DriveType.Network)
    {
        // network
    }   
}

just out of curiosity, would it be possible to tell if a network disc is a NAS programmatically?

No, that's the miracle of abstraction. You talk SMB to a device, it talks SMB back. Any device that can talk SMB can be on the other end, be it a Windows or Linux server, an desktop, a NAS, a SAN, heck, even routers and TVs talk SMB nowadays so you can plug in an USB drive and expose it over your home network.

You could detect a NAS by probing well-known ports where NASes are hosting extra services besides SMB, such as their (web) management interface and seeing what they return, but I wouldn't go there as I wouldn't know what problem that would solve.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272