Skip to main content

File Service

The File Service (IFileService) is a platform service that provides a unified API for file system operations across different file system providers. It supports local files, remote files, virtual file systems, and custom schemes.

Service Overview

The File Service is defined in src/vs/platform/files/common/files.ts:26 and acts as a registry and coordinator for file system providers.

Service Identifier

Dependency Injection

File Operations

Reading Files

URI
required
The file URI to read
IReadFileOptions
Reading options (position, length, limits, atomic)
CancellationToken
Cancellation token to abort the read operation

Writing Files

URI
required
The file URI to write
VSBuffer | VSBufferReadable | VSBufferReadableStream
required
The content to write
IWriteFileOptions
Writing options (overwrite, create, unlock, atomic, append)

File System Operations

File Metadata and Resolution

stat() - Get File Metadata

resolve() - List Directory Contents

File Watching

watch() - Watch for File Changes

createWatcher() - Correlated File Watching

Correlated watchers are more efficient as events are only delivered to listeners of that specific watcher, not broadcast to all file change listeners.

File System Providers

Registering a Provider

Provider Capabilities

Events

onDidFilesChange

onDidRunOperation

Error Handling

Common Use Cases

Reading Configuration Files

Watching Project Files

Best Practices

Stream Large Files: Use readFileStream() instead of readFile() for files larger than a few megabytes to avoid memory issues.
Atomic Operations: Use atomic writes for critical files to prevent corruption if the operation is interrupted.
Watch Carefully: Be specific with watch patterns and excludes to avoid performance issues from watching too many files.
Check Capabilities: Always check provider capabilities before attempting operations like trash or atomic writes.
Error Handling: Always handle file operation errors gracefully - files may not exist, be locked, or lack permissions.
  • ITextFileService - Higher-level service for text files with encoding support
  • IWorkspaceContextService - Provides workspace folder context
  • IEnvironmentService - Provides standard file paths

See Also