File Association Service
Overview
IFileAssociationService defines the application-level contract for querying supported file types and opening or creating files.
The default FileAssociationService implementation uses registered IFileHandler instances. It exposes their supported file types and dispatches each operation to one handler. Each handler can expose one or more FileTypeInfo entries. A descriptor contains the file type ID, title, extension, icon, and its open and create capabilities.
Default Implementation
The following dispatch rules belong to FileAssociationService. Another IFileAssociationService implementation may handle the same contract differently.
Opening and creating use different selectors:
Operation | Handler selector | Failure |
|---|---|---|
| The first handler whose |
|
| The handler that advertises a |
|
Both operations accept an optional CancellationToken. The dispatcher forwards it unchanged to the selected handler.
Opening a file
The default service sorts handlers once when it is created. Lower Priority values are evaluated first. It calls CanOpen(path) in that order and delegates to Open(path, cancel) as soon as a handler accepts the path. Later handlers are not evaluated.
Creating a file
The caller selects a FileTypeInfo from SupportedFiles and passes it to Create. The service matches only its Id, then forwards the path, descriptor, and cancellation token to the owning handler.
Registration
The file association service is included in the default core service registration. If an application composes core services explicitly, register it through the services builder:
Register each application-specific handler through the file association builder:
Handlers are registered as singletons. Their dependencies and their SupportedFiles collections should therefore be suitable for the application lifetime.
Example: Workspace Snapshot Files
Suppose an application can save its current workspace and later restore it. A small custom file format makes both operations available through IFileAssociationService.
The application-specific store contains the actual persistence logic:
The handler describes the .asvworkspace type and delegates persistence to that store:
Register both the store and the handler during application setup:
After registration, the default service exposes the snapshot descriptor through SupportedFiles and can dispatch open and create operations to WorkspaceSnapshotFileHandler.
Direct Usage
Inject IFileAssociationService when application code needs to open or create a file:
API
IFileAssociationService
Defines application-level operations for querying supported file types and opening or creating files.
Property | Type | Description |
|---|---|---|
|
| File types supported by the service. |
Method | Return Type | Description |
|---|---|---|
|
| Opens the specified file. |
|
| Creates a file of the specified type. |
IFileAssociationService.Open
Parameter | Type | Description |
|---|---|---|
|
| The path of the file to open. |
|
| A token that cancels the operation. |
IFileAssociationService.Create
Parameter | Type | Description |
|---|---|---|
|
| The path at which to create the file. |
|
| The file type to create. |
|
| A token that cancels the operation. |
IFileHandler
Defines an application-specific handler for one or more file types.
Property | Type | Description |
|---|---|---|
|
| Handler priority. |
|
| File types supported by the handler. |
Method | Return Type | Description |
|---|---|---|
|
| Determines whether the handler accepts a path. |
|
| Opens the specified file. |
|
| Creates the requested file type. |
IFileHandler.CanOpen
Parameter | Type | Description |
|---|---|---|
|
| The file-system path to inspect. |
IFileHandler.Open
Parameter | Type | Description |
|---|---|---|
|
| The path of the file to open. |
|
| A token that cancels the operation. |
IFileHandler.Create
Parameter | Type | Description |
|---|---|---|
|
| The path at which to create the file. |
|
| The file type to create. |
|
| A token that cancels the operation. |
FileTypeInfo
Describes a file type advertised by a handler.
FileTypeInfo constructor
Parameter | Type | Description |
|---|---|---|
|
| File type identifier. |
|
| Human-readable title of the file type. |
|
| File name extension, normally without a leading period. |
|
| Whether files of this type can be opened. |
|
| Whether files of this type can be created. |
|
| Optional icon associated with the file type. |
Property | Type | Description |
|---|---|---|
|
| File type identifier. |
|
| Human-readable title of the file type. |
|
| File name extension, normally without a leading period. |
|
| Whether files of this type can be opened. |
|
| Whether files of this type can be created. |
|
| Optional icon associated with the file type. |