jwebber
Posts: 8
Joined: Sun Jul 23, 2017 8:33 pm

Check Folder Path has Write and Display Error

Mon Jan 06, 2020 12:28 am

I have expanded the standard folder selection screen to also have another field for Data Folder. Under Published Events I added a "Check if a folder path has write permissions" that checks the folder and the screen doesn't continue if the user doesn't have write permissions. However, I want to give the user a dialog that tells them what the error is and why they can't continue. I built a CustomAction action for the message,but can't figure out how to call it only when the Check if a folder path has write permissions when the attribute is false.

Jason

Catalin
Posts: 6509
Joined: Wed Jun 13, 2018 7:49 am

Re: Check Folder Path has Write and Display Error

Wed Jan 08, 2020 5:11 pm

Hello Jason,

Unfortunately, what you want to achieve is not possible. Please allow me to explain why:

The Windows Installer documentation on the CheckExistingTargetPath Control Event states the following:
This event notifies the installer that it has to verify that the selected path is writable. If the path cannot be written, then the event blocks further ControlEvents associated with the control.
Basically, here, you want to spawn a message box (through an event), if the path is not writable. Unfortunately, as you can see above, all the events associated with the control will be blocked, meaning that it is not quite possible to spawn a message box if the path is not writable.

Fortunately, this can be achieved through a custom approach. For instance, we can have our own script which verifies if the path is writable and, if not, spawn a message box. I have created such a script for you to use:

Code: Select all

# add the assembly so we can use the Show() method of the MessageBox class
Add-Type -AssemblyName PresentationFramework

$testFolder = "C:\Program Files\WindowsApps"


# check if our folder is actually a folder

If (Test-Path $testFolder -PathType Container){

# create a temporary test file in the folder
$testTmpFile = "testFile"+[guid]::NewGuid()
$testFile = (Join-Path $testFolder $testTmpFile)
Write-Host $testFile


Try{

[io.file]::OpenWrite($testFile).close()
Write-Host -ForegroundColor Green "Writable"
$writable = $true

Remove-Item -ErrorAction SilentlyContinue $testFile

}

Catch {

Write-Host -ForegroundColor Red "Not writable"
$writable = $false

}

}

If ($writable -eq $true){

[System.Windows.MessageBox]::Show("The path is writable")

}

else{

[System.Windows.MessageBox]::Show("The path is not writable")

}
Basically, what we are doing in our script is:

- we set our path to the folder we want to check

- check the folder's path and if the folder is actually a folder

- we create a temporary file in that folder

- we use the File class with it's OpenWrite method to see if the file in the folder is writable

- if it's writable, spawn a message box stating that it is indeed writable

- if not, spawn a message box stating that it is not writable

As you can see, by default, I have used the %ProgramFiles%\WindowsApps folder, which is a hidden folder that is not writable.

Now that we have our script, here is how you can implement it in Advanced Installer:

1. please go to "Custom Actions" page

2. add a "PowerShellScriptInline" custom action without sequence (press the "Add custom action without sequence" button which is placed to the right side of the custom action's name)

3. copy paste the code provided above into the custom action

4. now please go to "Dialogs" page

5. select the "Next" button. Here, please remove the "Check if a folder path has write permissions" event and add a new one, as it follows:

Event: Execute Custom Action
Argument: your PowerShell script
Condition: leave default


This way, the message box should be spawned as per your request.

Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

a.guelle
Posts: 97
Joined: Tue May 19, 2015 2:23 pm

Re: Check Folder Path has Write and Display Error

Thu May 05, 2022 2:20 pm

And in case somone prefers VBS, here is a potential solution.

Code: Select all

' Checks if the MSI installer has write access to the DIR directory 
' and returns "false" in the property DIR_CHECK if it fails.

DIR = Session.Property("DIR")

If hasWriteAccessToFolder(DIR) Then
	Session.Property("DIR_CHECK") = "true" 
Else
	
	title = "Missing write permission"
	msg = "No write permission to the folder """ & DIR & """ or the folder does not exist."
	msgBox msg, vbCritical, title
	
	Session.Property("DIR_CHECK") = "false" 
End If


' Checks write permission to a given folder
'
' @param folderPath
'	Path to the folder
' @return <TRUE> if the Installer has write access, <FALSE> if not
Function hasWriteAccessToFolder(folderPath)
	
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
	If Not objFSO.FolderExists(folderPath) Then
		hasWriteAccessToFolder = False
        Exit Function
    End If

    'Build unique FilePath, DON'T WANT TO OVERWRITE SOMETHING THAT ALREADY EXISTS
    Dim count
	Do
        filePath = objFSO.BuildPath(folderPath, "TestWriteAccess" & count & ".tmp")
        count = count + 1
    Loop Until Not objFSO.FileExists(filePath)

    'Attempt to create test file
    On Error Resume Next
    objFSO.CreateTextFile(filePath).Write ("Test Folder Access")
    objFSO.DeleteFile(filePath)
	
	If Err.Number <> 0 Then
		hasWriteAccessToFolder = False
	Else
		hasWriteAccessToFolder = True
	End If
End FunctionI

Catalin
Posts: 6509
Joined: Wed Jun 13, 2018 7:49 am

Re: Check Folder Path has Write and Display Error

Thu May 05, 2022 3:15 pm

Hello,

Thank you very much for your followup on this and for sharing your solution with us!

I am sure this will be helpful for someone that prefers VBScript over PowerShell.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”