文章編號: 160529 - 上次校閱: 2007年1月19日 - 版次: 4.5

要傳回的網路磁碟機 UNC 路徑的範例程式碼

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。
進階: 須具備專家編碼、 互通性,與多使用者技能。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何使用應用程式 sub-procedure (或巨集) 的 Microsoft Visual Basic 和 Windows 應用程式發展介面 (API) 呼叫傳回通用命名慣例 (UNC) 路徑為對應的網路磁碟機。

其他相關資訊

Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證,包括但不是限於適售性以及適合某特定用途之默示擔保責任。本文假設您已熟悉我們所示範的程式設計語言,以及用來建立和偵錯程序的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您的特定需求。
如果您有限制程式設計經驗,您可以連絡 Microsoft 認證合作夥伴或 Microsoft 諮詢服務。如需詳細資訊請造訪下列 Microsoft] 網站:

Microsoft 認證合作夥伴-https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104)

Microsoft 摘要報告服務-http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice)

如需有關可用的支援選項,以及有關如何連絡 Microsoft 的詳細資訊,請造訪下列 Microsoft 網站: http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) UNC 是可以讓您使用某個網路資源例如網路伺服器,而不正式的說法是連線到網路資源與對應的磁碟機的命名慣例。UNC 路徑使用下列語法:
\ \ <server>\ <share>
其中 <server>是網路伺服器和 <share>的名稱是在伺服器上的資料夾。

對應的磁碟機使用的磁碟機代號 (,例如磁碟機 F:),其中字母代表伺服器和共用區,它會對應。

下列程式碼範例使用 Windows API 呼叫找不到對應的磁碟機,會回復其 UNC 路徑。

Microsoft Office 97 與 Microsoft Office 7.0

   ' 32-bit Function version.
   ' Enter this declaration on a single line.
   Declare Function WNetGetConnection32 Lib "MPR.DLL" Alias _
      "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal _
      lpszRemoteName As String, lSize As Long) As Long

   ' 32-bit declarations:
   Dim lpszRemoteName As String
   Dim lSize As Long

   ' Use for the return value of WNetGetConnection() API.
   Const NO_ERROR As Long = 0

   ' The size used for the string buffer. Adjust this if you
   ' need a larger buffer.
   Const lBUFFER_SIZE As Long = 255

   Sub GetNetPath()

      ' Prompt the user to type the mapped drive letter.
      DriveLetter = UCase(InputBox("Enter Drive Letter of Your Network" & _
         "Connection." & Chr(10) & "i.e. F (do not enter a colon)"))

      ' Add a colon to the drive letter entered.
      DriveLetter = DriveLetter & ":"

      ' Specifies the size in characters of the buffer.
      cbRemoteName = lBUFFER_SIZE

      ' Prepare a string variable by padding spaces.
      lpszRemoteName = lpszRemoteName & Space(lBUFFER_SIZE)

      ' Return the UNC path (\\Server\Share).
      lStatus& = WNetGetConnection32(DriveLetter, lpszRemoteName, _
         cbRemoteName)

      ' Verify that the WNetGetConnection() succeeded. WNetGetConnection()
      ' returns 0 (NO_ERROR) if it successfully retrieves the UNC path.
      If lStatus& = NO_ERROR Then

         ' Display the UNC path.
          MsgBox lpszRemoteName, vbInformation

      Else
         ' Unable to obtain the UNC path.
         MsgBox "Unable to obtain the UNC path.", vbInformation
      End If

   End Sub
				

Microsoft Excel 5.0

   ' 16-bit Function for Excel 5.0.
   ' Enter this declaration on a single line.
   Declare Function WNetGetConnection Lib "user" (ByVal lpszLocalName _
      As String, ByVal lpszRemoteName As String, cbRemoteName As _
      Integer) As Integer

   ' 16-bit declarations:
   Dim NetName As String
   Dim x As Integer
   Dim DriveLetter As String

   Sub GetNetPath()

      ' Prompt the user to type the mapped drive letter.
      DriveLetter = UCase(InputBox("Enter Drive Letter of Your Network" & _
         "Connection." & Chr(10) & "i.e. F (do not enter a colon)"))

      DriveLetter = DriveLetter & ":"

      ' 16-bit call for Excel 5.0.
      ' Pad NetName with spaces.
      NetName = NetName & Space(80)

      ' API call returns one of eight values. If it returns zero, it is
      ' successful.
      x = WNetGetConnection(DriveLetter, NetName, 80)

      ' Display the UNC path.
      MsgBox NetName

   End Sub
				

?考

取得更多資訊有關取得說明 Visual Basic forApplications 按一下面的文件編號,檢視 「 Microsoft 知識庫 」 中的發行項:
163435? (http://support.microsoft.com/kb/163435/EN-US/ ) VBA: 應用程式的 Visual Basic 程式設計資源

這篇文章中的資訊適用於:
  • Microsoft Access 97 Standard Edition
  • Microsoft Visual Basic for Applications 5.0
  • Microsoft Office 97 Standard Edition
  • Microsoft Office 95 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 5.0c
  • Microsoft Outlook 97 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Word 97 Standard Edition
關鍵字:?
kbmt kbcode kbhowto KB160529 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:160529? (http://support.microsoft.com/kb/160529/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。
Retired KB Article依現狀不再更新的知識庫內容免責聲明
本文旨在說明 Microsoft 不再提供支援的產品。因此,本文係依「現狀」提供,不會再更新。