简体   繁体   English

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

[英]How to check if user has write access to a folder?

I am trying to check all of the permissions that I can so that people can choose any file and before it fails later on in the program they can get an error message that directly responds to why they cannot save to that location.我正在尝试检查我所能获得的所有权限,以便人们可以选择任何文件,并且在程序稍后失败之前,他们可以获得一条错误消息,直接响应他们无法保存到该位置的原因。 The two that work right now that I have covered are "No Folder Selected," and "This File does NOT exist".我现在介绍的两个工作是“未选择文件夹”和“此文件不存在”。 Saying that it is readonly is not working and if anyone has any helpful tips that would be greatly appreciated or any ideas of more checks that I could do about the files.说它是只读的是行不通的,如果有人有任何有用的提示,将不胜感激,或者我可以对文件进行更多检查的任何想法。 I am testing it using the program files file on my computer.我正在使用计算机上的程序文件文件对其进行测试。

Sub CreateFile()

    Dim BaseDirectory As String
    Dim FS As FileSystemObject
    Set FS = New FileSystemObject

    BaseDirectory = GetFolder()

    If (BaseDirectory = vbNullString) Then
       MsgBox "No Folder Selected", vbExclamation, "Error"
       GoTo EndProgram
    End If

   'Not Working
    With FS.GetFolder(BaseDirectory)
        If (.Attributes And ReadOnly) Then
        MsgBox .Name & " is readonly!"
        GoTo EndProgram
        End If
    End With

    If Len(Dir(BaseDirectory)) = 0 Then
       MsgBox "This file does NOT exist."
       GoTo EndProgram
    End If

EndProgram:
End Sub

Function GetFolder() As String

    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
            sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function

I'm expecting this to say .Name is readonly!, but it does not work at .attributes and readonly.我期待这会说 .Name 是只读的!,但它不适用于 .attributes 和 readonly。 It just says This file does NOT exist它只是说这个文件不存在

Here is a function that checks if the current user has write access to a folder.这是一个检查当前用户是否对文件夹具有写入权限的函数。 It works by creating a temp file in that folder for writing, if it's able to create it then it will return true.它的工作原理是在该文件夹中创建一个临时文件用于写入,如果它能够创建它,那么它将返回 true。 Otherwise, this function will return false.否则,此函数将返回 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

Leverage the function?利用功能? I'm using VBS (not VBA) but someone may still find this a useful observation.我正在使用 VBS(不是 VBA),但有人可能仍然会发现这是一个有用的观察。 If you run the fso commands on a path that doesn't exist or perms issue it will return the function and error code, use that to determine if the user has access to that folder:如果在不存在或 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM