Joe M
Joe M

Reputation: 3440

Want to make a directory inaccessible with vb6

I'm working on a program that will disable access to shared folders on a document server if the hard drive is getting close to being full. Currently, I am just renaming them to something different to prevent the app server from being able to send more documents over. I'm wondering if there is a way to somehow lock down a folder programmatically, either setting it to read only, or disable its share status. From what I've seen, changing a folder to read-only directly in windows doesn't prevent new files from being copied into it. Anyone have any ideas on how to do this? My current code looks like this:

Private Function MoveShares(ByVal strOldLocation As String, ByVal strNewLocation As String) As Boolean

    Dim objFSO As New FileSystemObject
    If objFSO.FolderExists(strOldLocation) Then
        LogAction "Moving " & strOldLocation & " to " & strNewLocation
        objFSO.MoveFolder strOldLocation, strNewLocation
    End If
    Set objFSO = Nothing

End Function

Pretty basic, but I'm hoping I can do this in a subtler way.

Upvotes: 0

Views: 995

Answers (2)

Joe M
Joe M

Reputation: 3440

For the shared folder, I ended up editing the registry to "rename" the share itself, not the folder. I did this by reading the registry entry data, deleting the entry, and writing the data to a new entry. I also had to restart the computer browser and server services in order for the server to acquire the new share name. This prevents the app server from sending documents because it can't find a share with the old name. I also had to convert the security entry values from decimal back to hexadecimal before writing them to the new entry, and wait between stopping and restarting services to make sure they had finished before moving on the to next service.

Private Function RenameShare(ByVal strOldName As String, ByVal strNewName As String) As Boolean

Dim objRegAccess As Object
Dim varValues() As Variant
Dim varItem As Variant
Dim strSharePath As String
Dim strSecurityPath As String
Dim strValues As String
Dim strCmd As String

Set objRegAccess = CreateObject("Wscript.Shell")
strSharePath = "HKLM\SYSTEM\CurrentControlSet\services\LanmanServer\Shares\"
strSecurityPath = strSharePath & "Security\"
strValues = ""
varValues = objRegAccess.RegRead(strSharePath & strOldName)
strValues = ""
For Each varItem In varValues
    strValues = strValues & varItem & "~"
Next
RunCommand "REG ADD " & strSharePath & " /v " & strNewName & " /t REG_MULTI_SZ /s ~ /d " & strValues & " /f", False
RunCommand "REG DELETE " & strSharePath & " /v " & strOldName & " /f", False
strValues = ""
varValues = objRegAccess.RegRead(strSecurityPath & strOldName)
For Each varItem In varValues
    strValues = strValues & varItem & "~"
Next
strValues = ConvertDecToHex(strValues)
RunCommand "REG ADD " & strSecurityPath & " /v " & strNewName & " /t REG_BINARY /d " & strValues & " /f", False
RunCommand "REG DELETE " & strSecurityPath & " /v " & strOldName & " /f", False
RunCommand "NET STOP ""Computer Browser"" ", True
RunCommand "NET STOP ""Server"" ", True
RunCommand "NET START ""Server"" ", True
RunCommand "NET START ""Computer Browser"" ", False

End Function

Upvotes: 0

jac
jac

Reputation: 9726

googled this and found something similar on Daniweb, this is not my code so no warranties. I pasted the code below. Assuming this works try modifying permissions to deny the account(s) that are used by the apps. Deny permissions will override allowed permissions. You can look at the source here.

Dim strHomeFolder, strHome, strUser
Dim intRunError, objShell, objFSO
strHomeFolder = "C:\Test"
strUser="srikanth"

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strHomeFolder) Then
    intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " & strHomeFolder & " /t /c /g everyone:F ", 2, True)
    wscript.echo "The File " & strHomeFolder & ". Permissions changed to Every One."
    If intRunError <> 0 Then
        Wscript.Echo "Error assigning permissions for user " & strUser & " to home folder " & strHomeFolder
    End If
End If

Upvotes: 1

Related Questions