如何检查用户是否具有对文件夹的写权限?

时间:2019-06-13 15:21:53

标签: excel vba

我正在尝试检查所有可能的权限,以便人们可以选择任何文件,并且在程序以后稍后失败之前,他们会收到一条错误消息,直接响应他们为何无法保存到该位置。我现在介绍的两个可以正常工作的是“未选择文件夹”和“此文件不存在”。说它是只读的是行不通的,并且如果有人有任何有用的提示将不胜感激,或者有任何我可以对文件进行更多检查的想法。我正在使用计算机上的程序文件对它进行测试。

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    Dim client = New RestClient("https://api.ltl.xpo.com/imaging/1.0/shipments/951182326/imaged-docs?imageFormat=PDF&multiPartResp=False&imageType=BL")
    Dim request = New RestRequest(Method.[GET])
    request.AddHeader("cache-control", "no-cache")
    request.AddHeader("Connection", "keep-alive")
    request.AddHeader("accept-encoding", "gzip, deflate")
    request.AddHeader("Host", "api.ltl.xpo.com")
    request.AddHeader("Cache-Control", "no-cache")
    request.AddHeader("Accept", "*/*")
    request.AddHeader("Authorization", "Bearer " + accessToken)
    Dim response As IRestResponse = client.Execute(request)

    Dim regexResponse = Regex.Match(response.Content, "\{.*\:\{.*\:.*\}\}")

    Dim token As XPOImage = Newtonsoft.Json.JsonConvert.DeserializeObject(Of XPOImage)(regexResponse.ToString())

我希望这说.Name为只读!,但不适用于.attributes和readonly。只是说这个文件不存在

2 个答案:

答案 0 :(得分:1)

这是一项检查当前用户是否具有对文件夹的写访问权的功能。它通过在该文件夹中创建一个用于写入的临时文件来工作,如果能够创建它,则它将返回true。否则,此函数将返回false。

'CHECK TO SEE IF CURRENT USER HAS WRITE ACCESS TO FOLDER
Public Function HasWriteAccessToFolder(ByVal FolderPath As String) As Boolean

    '@example: HasWriteAccessToFolder("C:\Program Files") -> True || False

    'MAKE SURE FOLDER EXISTS, THIS FUNCTION RETURNS FALSE IF IT DOES NOT
    Dim Fso As Scripting.FileSystemObject
    Set Fso = New Scripting.FileSystemObject
    If Not Fso.FolderExists(FolderPath) Then
        Exit Function
    End If

    'GET UNIQUE TEMP FilePath, DON'T WANT TO OVERWRITE SOMETHING THAT ALREADY EXISTS
    Do
        Dim Count As Integer
        Dim FilePath As String

        FilePath = Fso.BuildPath(FolderPath, "TestWriteAccess" & Count & ".tmp")
        Count = Count + 1
    Loop Until Not Fso.FileExists(FilePath)

    'ATTEMPT TO CREATE THE TMP FILE, ERROR RETURNS FALSE
    On Error GoTo Catch
    Fso.CreateTextFile(FilePath).Write ("Test Folder Access")
    Kill FilePath

    'NO ERROR, ABLE TO WRITE TO FILE; RETURN TRUE!
    HasWriteAccessToFolder = True

Catch:

End Function

答案 1 :(得分:0)

利用功能?我正在使用 VBS(不是 VBA),但有人可能仍然会发现这是一个有用的观察。如果您在不存在的路径或 perms 问题上运行 fso 命令,它将返回函数和错误代码,使用它来确定用户是否有权访问该文件夹:

'VBS Example:

Function TestDirectory(FullDirPath)
'Purpose: test creation, if path doesn't exist or permissions issue, function will return error code
    strDir = fso.GetAbsolutePathName(FullDirPath)
    strDir = strDir & "\_randfoldercrtplsdelthis"
    fso.CreateFolder strDir
    If fso.FolderExists(strDir) Then
        fso.DeleteFolder strDir, TRUE
    End If
End Function

On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("WScript.Shell")

FilePath = "C:\Restricted Start Menu Locked\"
If TestDirectory(FilePath) <> 0 Then
    WScript.Echo "Folder Access Denied? Error = " & Err.Number
Else
    WScript.Echo "Woot!"
End If