PCLStorage.Standard
1.4.0-b2024060200
dotnet add package PCLStorage.Standard --version 1.4.0-b2024060200
NuGet\Install-Package PCLStorage.Standard -Version 1.4.0-b2024060200
<PackageReference Include="PCLStorage.Standard" Version="1.4.0-b2024060200" />
paket add PCLStorage.Standard --version 1.4.0-b2024060200
#r "nuget: PCLStorage.Standard, 1.4.0-b2024060200"
// Install PCLStorage.Standard as a Cake Addin #addin nuget:?package=PCLStorage.Standard&version=1.4.0-b2024060200&prerelease // Install PCLStorage.Standard as a Cake Tool #tool nuget:?package=PCLStorage.Standard&version=1.4.0-b2024060200&prerelease
PCL Storage
PCL Storage provides a consistent, portable set of local file IO APIs for .NET, Windows Phone, Windows Store, Xamarin.iOS, Xamarin.Android, and Silverlight. This makes it easier to create cross-platform .NET libraries and apps.
Here is a sample showing how you can use PCL Storage to create a folder and write to a text file in that folder:
public async Task PCLStorageSample()
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("answer.txt",
CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync("42");
}
Installation
Install the PCLStorage NuGet Package.
If you reference the package from a Portable Class Library, you will also need to reference the package from each platform-specific app. This is because the Portable Class Library version of PCL Storage doesn't contain the actual implementation of the storage APIs (because it differs from platform to platform), so referencing the package from an app will ensure that the platform-specific version of PCL Storage is included in the app and used at runtime.
Background information
Different .NET platforms have different APIs for accessing the file system or an app-local persisted storage area. The full .NET Framework provides the standard file and directory APIs (in the System.IO namespace), Silverlight and Windows Phone provide isolated storage APIs, and WinRT provides storage APIs in the Windows.Storage namespace.
These differing APIs make it harder to write cross-platform code. Traditionally, you could handle this via conditional compilation. However, that means you can't take advantage of Portable Class Libraries, and in any case may not scale well as your code gets complex (and especially because for WinRT you need to use async APIs).
Alternatively, you can create an abstraction for the functionality you need across platforms, and implement the abstraction for each platform you need to use. This approach allows you to use Portable Class Libraries, and in general makes your code cleaner and more maintainable by isolating the platform-specific pieces instead of having them sprinkled arbitrarily throughout your code.
Writing an abstraction layer is a bit of a barrier to entry to writing cross-platform code, and there's no reason everyone should have to do it separately for functionality as commonly needed as local file IO. PCL Storage aims to provide a common abstraction that is easy to take advantage of.
APIs
API documentation for PCL Storage can be found at NuDoq. The definitions for the main APIs in PCL Storage are below.
The primary APIs in PCL Storage are the IFile, IFolder, and IFileSystem interfaces. The APIs should be mostly self-explanatory and should feel very familiar if you have used the WinRT storage APIs.
The IFileSystem interface is the main API entry point. You can get an instance of the implementation for the current platform with the FileSystem.Current property.
namespace PCLStorage
{
public static class FileSystem
{
public static IFileSystem Current { get; }
}
public interface IFileSystem
{
IFolder LocalStorage { get; }
IFolder RoamingStorage { get; }
Task<IFile> GetFileFromPathAsync(string path);
Task<IFolder> GetFolderFromPathAsync(string path);
}
public enum CreationCollisionOption
{
GenerateUniqueName = 0,
ReplaceExisting = 1,
FailIfExists = 2,
OpenIfExists = 3,
}
public interface IFolder
{
string Name { get; }
string Path { get; }
Task<IFile> CreateFileAsync(string desiredName, CreationCollisionOption option);
Task<IFile> GetFileAsync(string name);
Task<IList<IFile>> GetFilesAsync();
Task<IFolder> CreateFolderAsync(string desiredName,
CreationCollisionOption option);
Task<IFolder> GetFolderAsync(string name);
Task<IList<IFolder>> GetFoldersAsync();
Task<ExistenceCheckResult> CheckExistsAsync(string name,
CancellationToken cancellationToken = default(CancellationToken));
Task DeleteAsync();
}
public enum FileAccess
{
Read,
ReadAndWrite
}
public interface IFile
{
string Name { get; }
string Path { get; }
Task<Stream> OpenAsync(FileAccess fileAccess);
Task DeleteAsync();
Task RenameAsync(string newName,
NameCollisionOption collisionOption = NameCollisionOption.FailIfExists,
CancellationToken cancellationToken = default(CancellationToken));
Task MoveAsync(string newPath,
NameCollisionOption collisionOption = NameCollisionOption.ReplaceExisting,
CancellationToken cancellationToken = default(CancellationToken));
}
public static class PortablePath
{
public static char DirectorySeparatorChar { get; }
public static string Combine(params string[] paths);
}
public static class FileExtensions
{
public static async Task<string> ReadAllTextAsync(this IFile file)
public static async Task WriteAllTextAsync(this IFile file, string contents);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.3 is compatible. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 1.3
- NETStandard.Library (>= 1.6.1)
- PCLStorage.Abstractions (>= 1.0.4-b2017032801)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.4.0-b2024060200 | 113 | 6/2/2024 |
1.0.0 | 96,586 | 5/20/2017 |