Class PhysicalFileSystem
Represents a file system that maps to a physical location on disk.
Inheritance
Namespace: Umbraco.Cms.Core.IO
Assembly: Umbraco.Core.dll
Syntax
public class PhysicalFileSystem : IPhysicalFileSystem, IFileSystem, IFileProviderFactory
Constructors
View SourcePhysicalFileSystem(IIOHelper, IHostingEnvironment, ILogger<PhysicalFileSystem>, string, string)
Initializes a new instance of the PhysicalFileSystem class.
Declaration
public PhysicalFileSystem(IIOHelper ioHelper, IHostingEnvironment hostingEnvironment, ILogger<PhysicalFileSystem> logger, string rootPath, string rootUrl)
Parameters
| Type | Name | Description |
|---|---|---|
| IIOHelper | ioHelper | The IO helper. |
| IHostingEnvironment | hostingEnvironment | The hosting environment. |
| ILogger<PhysicalFileSystem> | logger | The logger. |
| string | rootPath | The root path. |
| string | rootUrl | The root URL. |
Properties
View SourceCanAddPhysical
Gets a value indicating whether the filesystem can add/copy a file which is on a physical filesystem.
Declaration
public bool CanAddPhysical { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
In other words, whether the filesystem can copy/move a file that is on local disk, in a fast and efficient way.
Methods
View SourceAddFile(string, Stream)
Saves a file.
Declaration
public void AddFile(string path, Stream stream)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the file. |
| Stream | stream | A stream containing the file data. |
Remarks
Overrides the existing file, if any.
AddFile(string, Stream, bool)
Saves a file.
Declaration
public void AddFile(string path, Stream stream, bool overrideExisting)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the file. |
| Stream | stream | A stream containing the file data. |
| bool | overrideExisting | A value indicating whether to override the existing file, if any. |
Remarks
If a file exists and overrideExisting is false, an exception is thrown.
AddFile(string, string, bool, bool)
Adds a file.
Declaration
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the file. |
| string | physicalPath | The physical path of the file. |
| bool | overrideIfExists | A value indicating whether to override the existing file, if any. |
| bool | copy | A value indicating whether to copy the file, if true. |
Create()
Creates a file provider.
Declaration
public IFileProvider Create()
Returns
| Type | Description |
|---|---|
| IFileProvider | The file provider. |
DeleteDirectory(string)
Deletes a directory.
Declaration
public void DeleteDirectory(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the directory. |
DeleteDirectory(string, bool)
Deletes a directory.
Declaration
public void DeleteDirectory(string path, bool recursive)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the directory. |
| bool | recursive | A value indicating whether to recursively delete sub-directories. |
DeleteFile(string)
Deletes a file.
Declaration
public void DeleteFile(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the file. |
DirectoryExists(string)
Gets a value indicating whether a directory exists.
Declaration
public bool DirectoryExists(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the directory. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether a directory exists. |
EnsureDirectory(string)
Ensures that the directory exists.
Declaration
protected virtual void EnsureDirectory(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to the directory. |
EnsureDirectorySeparatorChar(string)
Ensures that the path ends with a directory separator character.
Declaration
protected string EnsureDirectorySeparatorChar(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to ensure. |
Returns
| Type | Description |
|---|---|
| string | The path with a directory separator character. |
EnsureTrailingSeparator(string)
Ensures that the path ends with a trailing separator.
Declaration
protected string EnsureTrailingSeparator(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to ensure. |
Returns
| Type | Description |
|---|---|
| string | The path with a trailing separator. |
EnsureUrlSeparatorChar(string)
Ensures that the path ends with a URL separator character.
Declaration
protected string EnsureUrlSeparatorChar(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to ensure. |
Returns
| Type | Description |
|---|---|
| string | The path with a URL separator character. |
FileExists(string)
Gets a value indicating whether a file exists.
Declaration
public bool FileExists(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the file. |
Returns
| Type | Description |
|---|---|
| bool | A value indicating whether the file exists. |
GetCreated(string)
Gets the created date of a directory or file.
Declaration
public DateTimeOffset GetCreated(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the directory or the file. |
Returns
| Type | Description |
|---|---|
| DateTimeOffset | The created date of the directory or the file. |
GetDirectories(string)
Gets directories in a directory.
Declaration
public IEnumerable<string> GetDirectories(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the directory. |
Returns
| Type | Description |
|---|---|
| IEnumerable<string> | The filesystem-relative path to the directories in the directory. |
Remarks
Filesystem-relative paths use forward-slashes as directory separators.
GetFiles(string)
Gets files in a directory.
Declaration
public IEnumerable<string> GetFiles(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the directory. |
Returns
| Type | Description |
|---|---|
| IEnumerable<string> | The filesystem-relative path to the files in the directory. |
Remarks
Filesystem-relative paths use forward-slashes as directory separators.
GetFiles(string, string)
Gets files in a directory.
Declaration
public IEnumerable<string> GetFiles(string path, string filter)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path of the directory. |
| string | filter | A filter. |
Returns
| Type | Description |
|---|---|
| IEnumerable<string> | The filesystem-relative path to the matching files in the directory. |
Remarks
Filesystem-relative paths use forward-slashes as directory separators.
GetFullPath(string)
Gets the full path.
Declaration
public string GetFullPath(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The full or filesystem-relative path. |
Returns
| Type | Description |
|---|---|
| string | The full path. |
Remarks
On the physical filesystem, the full path is the rooted (ie non-relative), safe (ie within this filesystem's root) path. All separators are Path.DirectorySeparatorChar.
GetLastModified(string)
Gets the last-modified date of a directory or file.
Declaration
public DateTimeOffset GetLastModified(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the directory or the file. |
Returns
| Type | Description |
|---|---|
| DateTimeOffset | The last modified date of the directory or the file. |
GetRelativePath(string)
Gets the filesystem-relative path of a full path or of an URL.
Declaration
public string GetRelativePath(string fullPathOrUrl)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fullPathOrUrl | The full path or URL. |
Returns
| Type | Description |
|---|---|
| string | The path, relative to this filesystem's root. |
Remarks
The relative path is relative to this filesystem's root, not starting with any directory separator. All separators are forward-slashes.
GetSize(string)
Gets the size of a file.
Declaration
public long GetSize(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the file. |
Returns
| Type | Description |
|---|---|
| long | The file of the size, in bytes. |
Remarks
If the file does not exist, returns -1.
GetUrl(string?)
Gets the URL.
Declaration
public string GetUrl(string? path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path. |
Returns
| Type | Description |
|---|---|
| string | The URL. |
Remarks
All separators are forward-slashes.
MoveFile(string, string, bool)
Moves a file.
Declaration
public void MoveFile(string source, string target, bool overrideIfExists = true)
Parameters
| Type | Name | Description |
|---|---|---|
| string | source | The filesystem-relative path to the source file. |
| string | target | The filesystem-relative path to the target file. |
| bool | overrideIfExists | A value indicating whether to override the existing file, if any. |
OpenFile(string)
Opens a file.
Declaration
public Stream OpenFile(string path)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The filesystem-relative path to the file. |
Returns
| Type | Description |
|---|---|
| Stream |
WithRetry(Action)
Retries an action with a retry count and a pause time.
Declaration
protected void WithRetry(Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The action to retry. |