# InstallerContext

Import: `import com.install4j.api.context.InstallerContext;`

```java
interface InstallerContext extends Context
```

The context provided by the installer.
In addition to the methods in the `Context` interface, this extended context offers methods that are specific
to the installer.

## Inherited from

- [Context](Context.md) — 75 methods

## Methods

| Signature | Returns | Description |
|---|---|---|
| [`setInstallationDirectory(File)`](#setinstallationdirectory-file) | `void` | Set the installation directory programmatically. |
| [`isUpdateInstallation()`](#isupdateinstallation) | `boolean` | Returns whether the installer is or will be updating an existing version of the same program. |
| [`getInstallerFile()`](#getinstallerfile) | `File` | Returns the current media file. |
| [`getMediaName()`](#getmedianame) | `String` | Get the name of the current media file. |
| [`installFile(File, File)`](#installfile-file-file) | `boolean` | Installs a file. |
| [`installFile(File, File, FileOptions)`](#installfile-file-file-fileoptions) | `boolean` | Installs a file. |
| [`installFile(File, File, FileOptions, ProgressInterface, int, int)`](#installfile-file-file-fileoptions-progressinterface-int-int) | `boolean` | Installs a file. |
| [`installDirectory(File, UninstallMode)`](#installdirectory-file-uninstallmode) | `void` | Creates a directory. |
| [`registerUninstallFile(File)`](#registeruninstallfile-file) | `void` | Registers an arbitrary file for uninstallation. |

### setInstallationDirectory(File)

```java
void setInstallationDirectory(File installationDirectory)
```

Set the installation directory programmatically. Based on the configuration in the media wizard, there is a default
installation directory. The "Installation directory" screen will suggest the location returned by
[`Context.getInstallationDirectory`](Context.md#getinstallationdirectory)
and subsequently call `setInstallationDirectory` with the user selection. To change the default installation directory,
you could call this method from a "Run script" action that is added to the "Startup" screen.


**Parameters:**

- `installationDirectory` — the new installation directory

### isUpdateInstallation()

```java
boolean isUpdateInstallation()
```

Returns whether the installer is or will be updating an existing version of the same program.
This method returns `true` if [`ApplicationRegistry.isUpdateDirectory`](../ApplicationRegistry.md#isupdatedirectory-file)
returns `true` for the currently set installation directory ([`InstallerContext.getInstallationDirectory`](InstallerContext.md#getinstallationdirectory).


**Returns:** `true` or `false`.

### getInstallerFile()

```java
File getInstallerFile()
```

Returns the current media file. On macOS it returns the base directory of the installer app bundle.


**Returns:** the current media file. Returns `null` if you debug the installer, since there is no installer file
in that case.

### getMediaName()

```java
String getMediaName()
```

Get the name of the current media file.


**Returns:** the original file name of the media file being installed without the file extension.

### installFile(File, File)

```java
boolean installFile(File sourceFile, File destFile) throws UserCanceledException
```

Installs a file. This is the same as calling [`InstallerContext.installFile`](InstallerContext.md#installfile-file-file-fileoptions)
with [`new FileOptions(sourceFile.lastModified())`](FileOptions.md#fileoptions-long).
The uninstaller will uninstall a file that has been installed with this method. The overwrite mode
will be `OverwriteMode.ALWAYS_ASK_EXCEPT_FOR_UPDATE` and the uninstaller mode
`UninstallMode.IF_CREATED`.


**Parameters:**

- `sourceFile` — the file to be installed.
- `destFile` — the destination file.

**Returns:** whether the installation was successful or not

**Throws:**

- `UserCanceledException` — if the user cancels the installation.

### installFile(File, File, FileOptions)

```java
boolean installFile(File sourceFile, File destFile, FileOptions options) throws UserCanceledException
```

Installs a file. This is the same as calling [`InstallerContext.installFile`](InstallerContext.md#installfile-file-file-fileoptions-progressinterface-int-int)
with a `ProgressInterface` of `null`. The
`FileOptions` parameter determines the overwrite and uninstall modes.


**Parameters:**

- `sourceFile` — the file to be installed.
- `destFile` — the destination file.
- `options` — the installation and uninstallation options.

**Returns:** whether the installation was successful or not

**Throws:**

- `UserCanceledException` — if the user cancels the installation.

### installFile(File, File, FileOptions, ProgressInterface, int, int)

```java
boolean installFile(File sourceFile, File destFile, FileOptions options, ProgressInterface progressInterface, int fromPercent, int toPercent) throws UserCanceledException
```

Installs a file. The method will try to copy the source file to the destination file. The
`FileOptions` parameter determines the overwrite mode and the uninstall mode.
With this method, the progress interface will be updated during the installation.
Use this method if you install very large files.

Note that the progress bar is only available if this method is called from an action and the action is associated
with a screen that offers a progress interface with a progress bar, such as the "Installation" screen or the
customizable "Display progress" screen.


**Parameters:**

- `sourceFile` — the file to be installed.
- `destFile` — the destination file.
- `options` — the installation and uninstallation options.
- `progressInterface` — the interface for displaying progress during the file installation.
The current progress interface can be obtained from [`Context.getProgressInterface`](Context.md#getprogressinterface).
- `fromPercent` — the start percentage value for the file installation. The progress interface
will be advanced starting with this percentage value.
- `toPercent` — the end percentage value for the file installation. The progress interface
will be advanced up to this percentage value.

**Returns:** whether the installation was successful or not

**Throws:**

- `UserCanceledException` — if the user cancels the installation.

### installDirectory(File, UninstallMode)

```java
void installDirectory(File directory, UninstallMode uninstallMode)
```

Creates a directory. The directory will be deleted by the uninstaller if all its containing files and directories are also uninstalled
and the `UninstallMode` requires an uninstallation. This method is only necessary if the directory will be empty and you do not know if the directory will
contain files. If you install a known set of files, you can just use the `installFile(...)` methods without creating the directory first.


**Parameters:**

- `directory` — the directory to be created. All parent directories will be created if necessary.
- `uninstallMode` — the behavior for uninstallation. It is recommended to use [`UninstallMode.IF_CREATED`](UninstallMode.md#if-created) by default.

### registerUninstallFile(File)

```java
void registerUninstallFile(File file)
```

Registers an arbitrary file for uninstallation.


**Parameters:**

- `file` — the file that should be removed when uninstalling the application.

