13

How do I convert a path with a drive letter like W:\Path\Share to the equivalent unc path like \\server\Share\File in .Net?

At the command prompt you can run net use and that will list the mappings. How do I get at that info in .Net?

Iain
  • 2,500
  • 1
  • 20
  • 24

3 Answers3

6

You can use P/Invoke and call on a native function such as WNetGetUniversalName.

On Freund
  • 4,376
  • 2
  • 23
  • 30
  • 4
    The WNetGetUniversalName function takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name. This only works for paths that you have already shared. It doesn't seem to work for local paths. ie if you are sharing d:\shared as \\mymachine\shared and you call WNetGetUniversalName it fails. – GrendleM Sep 22 '09 at 19:29
2

To get a list of the shared folders on the current machine in .NET you have to options:

Note that the calling process will have run in the context of a user with administrative privileges for this to work.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
0

To answer your question about net use here is a good solution Get UNC Path for Mapped Drive VB.net

I like the net use way.

Community
  • 1
  • 1
goku_da_master
  • 4,257
  • 1
  • 41
  • 43