2011-12-12 284 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.