Local or mapped path to UNC path( very urgent)

Joined
May 7, 2007
Messages
2
Reaction score
0
Hi,

I want to convert local or mapped path into UNC path.
I was able to get the code for mapped path but same code is not working for local drive.
Please provide me solution asap.
Curently i have following code:

[DllImport("mpr.dll")]

private static extern int WNetGetUniversalName (string lpLocalPath,

int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);

[
DllImport("mpr", CharSet=CharSet.Auto)]

protected static extern int WNetGetUniversalName (string lpLocalPath,

int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);

[
StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]

private struct UNIVERSAL_NAME_INFO

{

[
MarshalAs(UnmanagedType.LPTStr)]

public string lpUniversalName;

}

//

//Constants

//

protected const int NO_ERROR = 0;

protected const int ERROR_MORE_DATA = 234;

protected const int ERROR_NOT_CONNECTED = 2250;

protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;

//

//GetUNCPath function

//

public string GetUNCPath(string mappedDrive)

{

UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();

int bufferSize = Marshal.SizeOf(rni);

int nRet = WNetGetUniversalName(

mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,

ref rni, ref bufferSize);

if (ERROR_MORE_DATA == nRet)

{

IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;

try

{

nRet = WNetGetUniversalName(

mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,

pBuffer,
ref bufferSize);

if (NO_ERROR == nRet)

{

rni = (
UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer,typeof(UNIVERSAL_NAME_INFO));

}

}

finally

{

Marshal.FreeHGlobal(pBuffer);

}

}

switch (nRet)

{

case NO_ERROR:

return rni.lpUniversalName;

case ERROR_NOT_CONNECTED:

//Local file-name

MessageBox.Show("Share not connected");

return string.Empty;

default:

return string.Empty;

}

return string.Empty;

}

result:

Y: == \\remotemachine\folder1
C: == network not connected

Expected:

Y: == \\remotemachine\folder1
C: == \\MyMachineName\c$\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top