How to get the UNC share name of a given folder (if shared)?

  • Thread starter Thread starter José Joye
  • Start date Start date
J

José Joye

Hello,

Can someone could help me with the following:

I have a directory (e.g.: C:\myTest) and I want to retrieve the UNC share
name that has been assigned to it (or null if no share is defined for this
particular directory).

My first guess was to look at the DirectoryInfo Class. However, I could not
find anything.

Any help welcome!
José
 
I guess you'll need to enumerate the shares on the computer and see if any
of them refer to the given path. Take a look at the Win32_Share WMI class
and the System.Management namespace.

Arild
 
Arlid,

What about the WNetGetNetworkShare API function?

To the OP, check out the WNetGetUniversalName definition on
http://www.pinvoke.net. I've included a wrapper function which will set up
the buffers and extract the string from the structure passed to the API.

Hope this helps.
 
Nicholas,

Thanks a lot for the help. However, I think you misunderstood my problem (I
mean I was not clear...).

I went to the site you mentioned and have made a small test program to make
sure it does what I'm looking for:

string myUnc = GetUniversalName(@"c:\temp");
if (myUnc != null)
Console.WriteLine(@"share for c:\temp is {0}", myUnc);
else
Console.WriteLine(@"No share found for c:\temp");

By applying this code, I always get this error "This network connection does
not exist".
Reading the documentation of WNetGetUniversalName, I understand the meaning
of the error.
I'm passing a local drive that does not map to a network ressource (it is a
local drive).


What I'm trying to get is really the UNC name of a directory of one of my
local drives.
Thanks,
José

Nicholas Paldino said:
Arlid,

What about the WNetGetNetworkShare API function?

To the OP, check out the WNetGetUniversalName definition on
http://www.pinvoke.net. I've included a wrapper function which will set up
the buffers and extract the string from the structure passed to the API.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arild Bakken said:
I guess you'll need to enumerate the shares on the computer and see if any
of them refer to the given path. Take a look at the Win32_Share WMI class
and the System.Management namespace.

Arild
 
Nicholas,

WNetGetNetworkShare? Where'd you find that one?

And as I read the post, I assumed the OP had a local directory and wanted to
find out if, and under what name, that local directory was shared on the
network.


Arild


Nicholas Paldino said:
Arlid,

What about the WNetGetNetworkShare API function?

To the OP, check out the WNetGetUniversalName definition on
http://www.pinvoke.net. I've included a wrapper function which will set
up the buffers and extract the string from the structure passed to the
API.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arild Bakken said:
I guess you'll need to enumerate the shares on the computer and see if any
of them refer to the given path. Take a look at the Win32_Share WMI class
and the System.Management namespace.

Arild
 
My mistake. I assumed "local" to mean a local mapped drive (the
documentation for WNetGetNetworkShare uses this terminology as well).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
Arlid,

What about the WNetGetNetworkShare API function?

To the OP, check out the WNetGetUniversalName definition on
http://www.pinvoke.net. I've included a wrapper function which will set
up the buffers and extract the string from the structure passed to the
API.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arild Bakken said:
I guess you'll need to enumerate the shares on the computer and see if any
of them refer to the given path. Take a look at the Win32_Share WMI class
and the System.Management namespace.

Arild
 
Thanks a lot,

This works!

José

Arild Bakken said:
I guess you'll need to enumerate the shares on the computer and see if any
of them refer to the given path. Take a look at the Win32_Share WMI class
and the System.Management namespace.

Arild
 
Back
Top