简体   繁体   中英

Convert path to UNC path

I have tried this code to convert my path to UNC path:

[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
[return:MarshalAs(UnmanagedType.U4)]
static extern int WNetGetUniversalName(
    string lpLocalPath,
    [MarshalAs(UnmanagedType.U4)] int dwInfoLevel,
    IntPtr lpBuffer,
    [MarshalAs(UnmanagedType.U4)] ref int lpBufferSize);

const int UNIVERSAL_NAME_INFO_LEVEL = 0x00000001;
const int REMOTE_NAME_INFO_LEVEL = 0x00000002;

const int ERROR_MORE_DATA = 234;
const int NOERROR = 0;    

static string GetUniversalName(string localPath)
{
    // The return value.
    string retVal = null ;

    // The pointer in memory to the structure.
    IntPtr buffer = IntPtr.Zero;

    // Wrap in a try/catch block for cleanup.
    try
    {
        // First, call WNetGetUniversalName to get the size.
        int size = 0;

        // Make the call.
        // Pass IntPtr.Size because the API doesn't like null, even though
        // size is zero.  We know that IntPtr.Size will be
        // aligned correctly.
        int apiRetVal = WNetGetUniversalName(localPath, UNIVERSAL_NAME_INFO_LEVEL, (IntPtr) IntPtr.Size, ref size);

        // If the return value is not ERROR_MORE_DATA, then
        // raise an exception.
        if (apiRetVal != ERROR_MORE_DATA)
            // Throw an exception.
            throw new Win32Exception(apiRetVal);

        // Allocate the memory.
        buffer = Marshal.AllocCoTaskMem(size);

        // Now make the call.
        apiRetVal = WNetGetUniversalName(localPath, UNIVERSAL_NAME_INFO_LEVEL, buffer, ref size);

        // If it didn't succeed, then throw.
        if (apiRetVal != NOERROR)
            // Throw an exception.
            throw new Win32Exception(apiRetVal);

        // Now get the string.  It's all in the same buffer, but
        // the pointer is first, so offset the pointer by IntPtr.Size
        // and pass to PtrToStringAnsi.
        retVal = Marshal.PtrToStringAuto(new IntPtr(buffer.ToInt64() + IntPtr.Size), size);
        retVal = retVal.Substring(0, retVal.IndexOf('\0'));
    }
    finally
    {
        // Release the buffer.
        Marshal.FreeCoTaskMem(buffer);
    }

    // First, allocate the memory for the structure.

    // That's all folks.
    return retVal;
}

But when I send the path \\\\myservername\\sharedfoldername to this method i receive this error:

The specified device name is not valid.

What is my mistake?

From MSDN (emphasis mine):

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.

You're passing in a UNC path, but the function expects a drive-based path (ie something like X:\\foo\\bar ) and will then return a UNC path.

The specified device name is not valid.

As far as I can remember, when WNetGetUniversalName() got the path of local drive, it also returned ERROR_BAD_DEVICE_PATH.

So, three years ago, I understood this return code as below;

ERROR_BAD_DEVICE_PATH :
Please check whether the path is really needed to change into UNC name or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM