• Detecting file/folder permissions (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Detecting file/folder permissions (VB6)

    Author
    Topic
    #415963

    I’m trying to programatically detect permissions on a file/folder (folder specifically in this case). I think it has something to do with GetNamedSecurityInfo but I can only seem to find C examples.
    Does anyone have any experience using this API in VB? Any help would be much appreciated.

    Regards,

    Viewing 2 reply threads
    Author
    Replies
    • #929129

      This looks to be a relevant article, but the code lines don’t break properly when viewed with Firefox, so I can’t be sure: How To Use High-Level Access Control APIs from Visual Basic (MSKB 295004).

      • #929199

        Thanks for the reply jscher2000. That code seems to set the permissions, which is tempting as it would solve my problems, but I don’t think our customers would appreciate me messing with their security settings!

        I’m currently trying to see if any of the processes in the code are used to Get the security first, but no luck so far!

        I can’t believe its so complicated to write a dll to copy a file to a destination silently and capture the errors for display opposed to displaying message boxes! Its a shame “it failed for some reason” doesnt cut it as a message!

        • #929218

          Phil,
          How are you copying the file? Can’t you just test the number of the error raised when you try the copy – e.g. 70 = Permission denied?

          • #929221

            That was my first thought…..

            I am using the SHFileOperation API. In usual circumstances it works fine, as “proper” message boxes are returned on error.
            However, I am programming for an automated process which then writes the errors to a log file, therefore running the API in Silent mode – which is a problem because whatever the error SHFileOperation encounters, it returns 1223 as its code!

            NOTE: Just thought I should add that im writing an updater program for our main product so i’m trying to use little if any extra files (dll’s etc). Seeing as the whole process can be done in windows manually the theory is that this should be possible, but it isn’t proving easy due to weird returns from APIs such as the above.

            The main reasons it fails have either been lack of write permissions, 0 hard disk space or no quota space. The quota space has also been a problem because Windows as standard returns a “no disk space” error when Quota has been reached, with no mention of Quota’s or profiles – this has led to confusion with our customers – think I’ve sorted it to a certain extent with the exception of when the user doesnt have permission to read quota’s! Writing files to a disk isn’t so simple after all!

            • #929254

              just try to write a small dummy text file to test for proper access rights. then depending on the result, abort or continue with the actual file you want to write.

            • #929255

              The problem still exists in that I can’t tell the customer what has gone wrong.

              An example: I attempt to write temp.txt to the temp folder. Permissions is denied so the API returned 1223 – exactly the same as any other error that occurs – I won’t be able to detect which error has occured.

              Thanks anyway.

            • #929268

              no, you have to write it with the standard open, print, close what have you staments or the file system object. then you can test silently for access denied. then write the actual file using the api.

    • #929433

      I would use the FileSystemObject. First, set a reference to Microsoft Scripting Runtime and then use code like this:

          Dim fso As New Scripting.FileSystemObject
          Dim fld As Scripting.Folder
          Set fld = fso.GetFolder(sPath)
          If fld.Attributes And ReadOnly Then

      You can see the help for the FileSystemObject at http://msdn.microsoft.com/library/default….oattributes.asp[/url]
      HTH –Sam

    • #929436

      If you really want to use API calls, then I think that you want GetFileSecurity, see http://www.mentalis.org/apilist/GetFileSecurity.shtml%5B/url%5D for documentation. Also, it looks like http://www.freevbcode.com/ShowCode.asp?ID=4318%5B/url%5D is exactly the code that you need (for Access Rights, not Vipers, sorry), but it sure makes you appreciate the FileSystemObject! Have fun! –Sam

      • #929485

        Wow – thanks for all of your replies.
        Pieter, it didnt occur to me to try using the standard open, print, close commands! My mind was set with the API – i’ll give that a go.

        The reason I didnt go down the route of FSO is that I was taught that FSO was considerably slower than API and shouldnt get in the habit of using it if there is an alternative. I’m not sure how much fact is behind that statement – I have seen a timed test to prove it but I cant remember what it tested on.
        But if I really can’t do the API method, i’ll give those links a go, thanks Sam and Hans.

        In the meantime, i’m going to give Pieters method a try and take a look at Sams API links so I can at least finish the permission modules I started, even if I dont use them in this instance.

        I’ll let you know how I get on. Thanks again for all of your help.

    Viewing 2 reply threads
    Reply To: Detecting file/folder permissions (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: