2011-12-12 276 views
1

我曾尝试这个代码到我的路径转换为UNC路径:将路径转换为UNC路径

[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; 
} 

但是当我发送给此方法的路径\\myservername\sharedfoldername我收到此错误:

The specified device name is not valid.

我的错误是什么?

+1

似乎有点不公平投票这篇文章下来...这家伙只是不知道如何使用此代码,并要求像堆栈溢出其他人一样的帮助。 – Sheridan

+0

谢谢谢里登。 –

回答

6

MSDN(重点煤矿):

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.

你传递一个UNC路径,但该函数需要基于驱动器的路径(即像X:\foo\bar),并随后将返回 UNC路径。

+0

谢谢,那么我应该怎么做才能获得我的路径的UNC路径?有没有什么方法? –

+2

我不明白你的问题......'\\ myservername \ sharedfoldername' *是一个UNC路径。你到底想做什么? – Arnout

+0

我在这个路径中有一个文件,我想在autocad中打开它,但我收到了这个错误:“File \\ servername \ sharedfoldername \ protectedfoldername \ File1.dwg”找不到。有人告诉我将我的路径转换为UNC version.Please帮我吗? –

1

The specified device name is not valid.

至于我还记得,当WNetGetUniversalName()得到本地驱动器的路径,它也返回ERROR_BAD_DEVICE_PATH。

因此,三年前,我了解了如下的返回代码;

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