0

A network drive is mapped on our application server and drive name is Z. When I am trying to get drives information am not getting Mapped drive (Z). Tried with below code:

 DriveInfo[] allDrives = DriveInfo.GetDrives();
 Response.Write("Drives Present in Server: </br>");
 foreach (DriveInfo d in allDrives)
 {
     Response.Write("---Drive Name: " + d.Name + " Drive Type: " + d.DriveType + "</br>");                
 }

How do i get this mapped drive.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
user1463065
  • 563
  • 2
  • 11
  • 30
  • https://stackoverflow.com/questions/12029855/why-does-driveinfo-getdrives-not-get-network-mapped-drive-in-windows-service – Roman Ryzhiy Jan 20 '21 at 09:04
  • https://social.msdn.microsoft.com/Forums/vstudio/en-US/e5b3ee65-4d08-4ee5-bba5-60a2a5b50d07/why-driveinfogetdrives-not-getting-networkmapped-drive-in-windows-service-which-i-developed-in – Roman Ryzhiy Jan 20 '21 at 09:05
  • 1
    There's no such thing as a network drive. Mapped drives aren't real drives, they're (roughly) user-specific shortcuts that point to shared folders on a remote server. Why don't you use the actual shared folder path? – Panagiotis Kanavos Jan 20 '21 at 09:09
  • Windows does have a [distributed file system](https://learn.microsoft.com/en-us/windows-server/storage/dfs-namespaces/dfs-overview), which doesn't work through mapped drives at all. – Panagiotis Kanavos Jan 20 '21 at 09:10
  • @Panagiotis Kanavos Not exactly. At least with the .NET point of view. `System.IO.DriveType` enum has an item `Network` for network drives. – Mustafa Özçetin Apr 11 '23 at 07:42
  • That doesn't mean it's a "network drive" or that such a concept exists, or that `Z:` is accessible from other accounts or web apps – Panagiotis Kanavos Apr 11 '23 at 08:23
  • The letter `Z:` maps to a UNC path , eg `\\thatserver\thatfolder` that can be used directly from any account that has permissions to access that path. There's no need to use a mapped drive – Panagiotis Kanavos Apr 11 '23 at 08:29

1 Answers1

0

Your code should work for mapped drives as well. Just ensure that connection to the network is proper (eg., if VPN should be connected to access the network drive).

I'm having similar code and it was crashing when iterating through the mapped Z drive when VPN connection was lost. Also, to avoid crashing in deployments, add try-catch blocks.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16
Nin
  • 11
  • 5
  • Yes, when tried, the OP's code displays the network drives. Try-catch blocks are necessary, too. – Mustafa Özçetin Apr 11 '23 at 07:53
  • It works only because the drive is mapped in the user's account. Running that code in a web app will fail. Using the actual UNC path the drive maps to would still work. This has nothing to do with VPNs either. VPNs don't establish mapped drives. You'd only need an active VPN if you wanted to connect to a work network remotely and then map a drive on the work network. – Panagiotis Kanavos Apr 11 '23 at 08:23