将共享文件夹路径转换为UNC路径 [英] Convert Shared folder path to UNC path

查看:349
本文介绍了将共享文件夹路径转换为UNC路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用计算机名称来处理当前路径,从而将当前共享文件夹路径转换为unc路径。但是会导致编译错误:公共函数UNCpath()中 elem = UBound(CurrentPathA)行上的预期数组。你们能告诉我造成此问题的似乎是什么问题吗?也许分享更好的主意以获取unc路径?

I'm trying to convert current shared folder path to unc path by manipulating current path with computer name. However result in compile error: expected array on the line"elem = UBound(CurrentPathA)" in Public Function UNCpath(). Can you guys tell me what seems to be the problem causing this issue? and perhaps share better idea to get unc path?

    Option Explicit

    #If VBA7 Then
    Private Declare PtrSafe Function fnGetComputerName Lib "kernel32" Alias "GetComputerNameW" (ByVal lpBuffer As LongPtr, ByRef nSize As Long) As Long
    #Else
    Private Declare Function fnGetComputerName Lib "kernel32" Alias "GetComputerNameW" (ByVal lpBuffer As Long, ByRef nSize As Long) As Long
    #End If

    Public Function GetComputerName() As String
    Const MAX_COMPUTERNAME_LENGTH As Long = 31

    Dim buf As String, buf_len As Long

    buf = String$(MAX_COMPUTERNAME_LENGTH + 1, 0)
    buf_len = Len(buf)

    If (fnGetComputerName(StrPtr(buf), buf_len)) = 0 Then
    GetComputerName = "ErrorGettingComputerName"
    Else
    GetComputerName = Left$(buf, buf_len)
    End If
    End Function


    Public Function UNCpath() As String
    Dim CompName As String, CurrentPath As String, CurrentPathA As String

    CompName = GetComputerName()
    CurrentPath = ThisWorkbook.Path
    CurrentPathA = Split(CurrentPath, "\")
    elem = UBound(CurrentPathA)
    CurrentPath = CurrentPathA(elem)
    UNCpath = "\\" & CompName & "\" & CurrentPath
    End Function


推荐答案

或使用文件系统对象:

Function GetUNCLateBound(strMappedDrive As String) As String

    Dim objFso  As Object
    Set objFso = CreateObject("Scripting.FileSystemObject")

    Dim strDrive As String
    Dim strShare As String

    'Separate the mapped letter from
    'any following sub-folders
    strDrive = objFso.GetDriveName(strMappedDrive)

    'find the UNC share name from the mapped letter
    strShare = objFso.Drives(strDrive & "\").ShareName

    'The Replace function allows for sub-folders
    'of the mapped drive
    GetUNCLateBound = Replace(strMappedDrive, strDrive, strShare)

    Set objFso = Nothing 'Destroy the object

End Function

我刚刚发现并修改了这笔信用,该信用应归于所有:

I just found and modified this, to which the credit is due, to be late-bound:

http://pagecommunication.co.uk/2014/07/vba-to-convert-a-mapped-drive-letter-to-unc-path/

这篇关于将共享文件夹路径转换为UNC路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆