You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/file-providers.md
+39-37Lines changed: 39 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Learn how ASP.NET Core abstracts file system access through the use
5
5
monikerRange: '>= aspnetcore-2.1'
6
6
ms.author: riande
7
7
ms.custom: mvc
8
-
ms.date: 11/07/2019
8
+
ms.date: 04/06/2020
9
9
uid: fundamentals/file-providers
10
10
---
11
11
# File Providers in ASP.NET Core
@@ -14,9 +14,9 @@ By [Steve Smith](https://ardalis.com/)
14
14
15
15
::: moniker range=">= aspnetcore-3.0"
16
16
17
-
ASP.NET Core abstracts file system access through the use of File Providers. File Providers are used throughout the ASP.NET Core framework:
17
+
ASP.NET Core abstracts file system access through the use of File Providers. File Providers are used throughout the ASP.NET Core framework. For example:
18
18
19
-
*`IWebHostEnvironment` exposes the app's [content root](xref:fundamentals/index#content-root) and [web root](xref:fundamentals/index#web-root) as `IFileProvider` types.
19
+
*<xref:Microsoft.AspNetCore.Hosting.IWebHostEnvironment> exposes the app's [content root](xref:fundamentals/index#content-root) and [web root](xref:fundamentals/index#web-root) as `IFileProvider` types.
20
20
*[Static File Middleware](xref:fundamentals/static-files) uses File Providers to locate static files.
21
21
*[Razor](xref:mvc/views/razor) uses File Providers to locate pages and views.
22
22
* .NET Core tooling uses File Providers and glob patterns to specify which files should be published.
@@ -39,32 +39,33 @@ The primary interface is <xref:Microsoft.Extensions.FileProviders.IFileProvider>
39
39
*<xref:Microsoft.Extensions.FileProviders.IFileInfo.Length> (in bytes)
40
40
*<xref:Microsoft.Extensions.FileProviders.IFileInfo.LastModified> date
41
41
42
-
You can read from the file using the [IFileInfo.CreateReadStream](xref:Microsoft.Extensions.FileProviders.IFileInfo.CreateReadStream*) method.
42
+
You can read from the file using the <xref:Microsoft.Extensions.FileProviders.IFileInfo.CreateReadStream*?displayProperty=nameWithType> method.
43
43
44
-
The sample app demonstrates how to configure a File Provider in `Startup.ConfigureServices` for use throughout the app via [dependency injection](xref:fundamentals/dependency-injection).
44
+
The *FileProviderSample*sample app demonstrates how to configure a File Provider in `Startup.ConfigureServices` for use throughout the app via [dependency injection](xref:fundamentals/dependency-injection).
45
45
46
46
## File Provider implementations
47
47
48
-
Three implementations of `IFileProvider` are available.
48
+
The following table lists implementations of `IFileProvider`.
49
49
50
50
| Implementation | Description |
51
51
| -------------- | ----------- |
52
-
|[PhysicalFileProvider](#physicalfileprovider)|The physical provider is used to access the system's physical files. |
53
-
|[ManifestEmbeddedFileProvider](#manifestembeddedfileprovider)|The manifest embedded provider is used to access files embedded in assemblies. |
54
-
|[CompositeFileProvider](#compositefileprovider)|The composite provider is used to provide combined access to files and directories from one or more other providers. |
52
+
|[CompositeFileProvider](#compositefileprovider)|Used to provide combined access to files and directories from one or more other providers. |
53
+
|[ManifestEmbeddedFileProvider](#manifestembeddedfileprovider)|Used to access files embedded in assemblies. |
54
+
|[PhysicalFileProvider](#physicalfileprovider)|Used to access the system's physical files. |
55
55
56
56
### PhysicalFileProvider
57
57
58
58
The <xref:Microsoft.Extensions.FileProviders.PhysicalFileProvider> provides access to the physical file system. `PhysicalFileProvider` uses the <xref:System.IO.File?displayProperty=fullName> type (for the physical provider) and scopes all paths to a directory and its children. This scoping prevents access to the file system outside of the specified directory and its children. The most common scenario for creating and using a `PhysicalFileProvider` is to request an `IFileProvider` in a constructor through [dependency injection](xref:fundamentals/dependency-injection).
59
59
60
-
When instantiating this provider directly, a directory path is required and serves as the base path for all requests made using the provider.
60
+
When instantiating this provider directly, an absolute directory path is required and serves as the base path for all requests made using the provider. Glob patterns aren't supported in the directory path.
61
61
62
-
The following code shows how to create a `PhysicalFileProvider` and use it to obtain directory contents and file information:
62
+
The following code shows how to use `PhysicalFileProvider` to obtain directory contents and file information:
The File Provider can be used to iterate through the directory specified by `applicationRoot` or call `GetFileInfo` to obtain a file's information. The File Provider has no access outside of the `applicationRoot` directory.
77
+
The File Provider can be used to iterate through the directory specified by `applicationRoot` or call `GetFileInfo` to obtain a file's information. Glob patterns can't be passed to the `GetFileInfo` method. The File Provider has no access outside of the `applicationRoot` directory.
77
78
78
-
The sample app creates the provider in the app's `Startup.ConfigureServices`class using [IHostingEnvironment.ContentRootFileProvider](xref:Microsoft.Extensions.Hosting.IHostingEnvironment.ContentRootFileProvider):
79
+
The *FileProviderSample*sample app creates the provider in the `Startup.ConfigureServices`method using <xref:Microsoft.Extensions.Hosting.IHostingEnvironment.ContentRootFileProvider?displayProperty=nameWithType>:
79
80
80
81
```csharp
81
82
varphysicalProvider=_env.ContentRootFileProvider;
@@ -85,15 +86,16 @@ var physicalProvider = _env.ContentRootFileProvider;
85
86
86
87
The <xref:Microsoft.Extensions.FileProviders.ManifestEmbeddedFileProvider> is used to access files embedded within assemblies. The `ManifestEmbeddedFileProvider` uses a manifest compiled into the assembly to reconstruct the original paths of the embedded files.
87
88
88
-
Add a package reference to the project for the [Microsoft.Extensions.FileProviders.Embedded](https://www.nuget.org/packages/Microsoft.Extensions.FileProviders.Embedded) package.
89
+
To generate a manifest of the embedded files:
89
90
90
-
To generate a manifest of the embedded files, set the `<GenerateEmbeddedFilesManifest>` property to `true`. Specify the files to embed with [\<EmbeddedResource>](/dotnet/core/tools/csproj#default-compilation-includes-in-net-core-projects):
91
+
1. Add the [Microsoft.Extensions.FileProviders.Embedded](https://www.nuget.org/packages/Microsoft.Extensions.FileProviders.Embedded) NuGet package to your project.
92
+
1. Set the `<GenerateEmbeddedFilesManifest>` property to `true`. Specify the files to embed with [\<EmbeddedResource>](/dotnet/core/tools/csproj#default-compilation-includes-in-net-core-projects):
Use [glob patterns](#glob-patterns) to specify one or more files to embed into the assembly.
95
97
96
-
The sample app creates an `ManifestEmbeddedFileProvider` and passes the currently executing assembly to its constructor.
98
+
The *FileProviderSample*sample app creates an `ManifestEmbeddedFileProvider` and passes the currently executing assembly to its constructor.
97
99
98
100
*Startup.cs*:
99
101
@@ -118,24 +120,29 @@ Additional overloads allow you to:
118
120
119
121
The <xref:Microsoft.Extensions.FileProviders.CompositeFileProvider> combines `IFileProvider` instances, exposing a single interface for working with files from multiple providers. When creating the `CompositeFileProvider`, pass one or more `IFileProvider` instances to its constructor.
120
122
121
-
In the sample app, a `PhysicalFileProvider` and a `ManifestEmbeddedFileProvider` provide files to a `CompositeFileProvider` registered in the app's service container:
123
+
In the *FileProviderSample*sample app, a `PhysicalFileProvider` and a `ManifestEmbeddedFileProvider` provide files to a `CompositeFileProvider` registered in the app's service container. The following code is found in the project's `Startup.ConfigureServices` method:
The [IFileProvider.Watch](xref:Microsoft.Extensions.FileProviders.IFileProvider.Watch*) method provides a scenario to watch one or more files or directories for changes. `Watch` accepts a path string, which can use [glob patterns](#glob-patterns) to specify multiple files. `Watch`returns an <xref:Microsoft.Extensions.Primitives.IChangeToken>. The change token exposes:
129
+
The <xref:Microsoft.Extensions.FileProviders.IFileProvider.Watch*?displayProperty=nameWithType> method provides a scenario to watch one or more files or directories for changes. The `Watch`method:
128
130
129
-
*<xref:Microsoft.Extensions.Primitives.IChangeToken.HasChanged>– A property that can be inspected to determine if a change has occurred.
130
-
*<xref:Microsoft.Extensions.Primitives.IChangeToken.RegisterChangeCallback*>– Called when changes are detected to the specified path string. Each change token only calls its associated callback in response to a single change. To enable constant monitoring, use a <xref:System.Threading.Tasks.TaskCompletionSource`1> (shown below) or recreate `IChangeToken` instances in response to changes.
131
+
*Accepts a file path string, which can use [glob patterns](#glob-patterns)to specify multiple files.
132
+
*Returns an <xref:Microsoft.Extensions.Primitives.IChangeToken>.
131
133
132
-
In the sample app, the *WatchConsole* console app is configured to display a message whenever a text file is modified:
134
+
The resulting change token exposes:
135
+
136
+
*<xref:Microsoft.Extensions.Primitives.IChangeToken.HasChanged>–A property that can be inspected to determine if a change has occurred.
137
+
*<xref:Microsoft.Extensions.Primitives.IChangeToken.RegisterChangeCallback*>–Called when changes are detected to the specified path string. Each change token only calls its associated callback in response to a single change. To enable constant monitoring, use a <xref:System.Threading.Tasks.TaskCompletionSource`1> (shown below) or recreate `IChangeToken` instances in response to changes.
Some file systems, such as Docker containers and network shares, may not reliably send change notifications. Set the `DOTNET_USE_POLLING_FILE_WATCHER` environment variable to `1` or `true` to poll the file system for changes every four seconds (not configurable).
137
144
138
-
## Glob patterns
145
+
###Glob patterns
139
146
140
147
File system paths use wildcard patterns called *glob (or globbing) patterns*. Specify groups of files with these patterns. The two wildcard characters are `*` and `**`:
141
148
@@ -145,19 +152,14 @@ Matches anything at the current folder level, any filename, or any file extensio
145
152
**`**`**
146
153
Matches anything across multiple directory levels. Can be used to recursively match many files within a directory hierarchy.
147
154
148
-
**Glob pattern examples**
149
-
150
-
**`directory/file.txt`**
151
-
Matches a specific file in a specific directory.
152
-
153
-
**`directory/*.txt`**
154
-
Matches all files with *.txt* extension in a specific directory.
155
-
156
-
**`directory/*/appsettings.json`**
157
-
Matches all `appsettings.json` files in directories exactly one level below the *directory* folder.
155
+
The following table provides common examples of glob patterns.
158
156
159
-
**`directory/**/*.txt`**
160
-
Matches all files with *.txt* extension found anywhere under the *directory* folder.
157
+
|Pattern |Description |
158
+
|---------|---------|
159
+
|`directory/file.txt`|Matches a specific file in a specific directory.|
160
+
|`directory/*.txt`|Matches all files with *.txt* extension in a specific directory.|
161
+
|`directory/*/appsettings.json`|Matches all *appsettings.json* files in directories exactly one level below the *directory* folder.|
162
+
|`directory/**/*.txt`|Matches all files with a *.txt* extension found anywhere under the *directory* folder.|
0 commit comments