-
Notifications
You must be signed in to change notification settings - Fork 26
Quickstart
Add the Platform.VirtualFileSystem and Platform.VirtualFileSystem.Providers.Zip packages using the Visual Studio NuGet manager. This should add the following assembly references to you project: Platform.dll, Platform.Xml.Serialization, Platform.VirtualFileSystem.dll, Platform.VirtualFileSystem.Providers.Zip and ICSharpCode.SharpZipLib.
- INodeResolver - Provides basic access to nodes via URLs or relative paths. Relative paths can include
".."and"." - INode - Basic element of a file system. Also extends INodeResolver
- IFile - Extends INode and represents a file
- IDirectory - Extends INode and represents a directory
- INodeContent - Providers access to the content of a file (
Streams,TextReaders,TextWriters). Usually retrived usingINode.GetContent() - IFileSystem - Extends INodeResolver and provides access to files and directories form a single files sytem
- IFileSystemManager - Extends INodeResolver and provides access to files and directories from multiple file system
Access the default FileSystemManager and resolve your first directory:
using System;
using Platform.VirtualFileSystem;
public static void Main()
{
var windowsDirectory = FileSystemManager.Default.ResolveDirectory("C:/Windows");
}
Platform.VirtualFileSystem resolves directories using URLs but absolute local file system paths are also supported. Examples of valid paths to resolve include: file://c:/Windows, C:\Windows, /usr/local/src (on Unix) and http://www.github.com. Although you can use backslashes instead of forward slashes to separate path components on windows, it's best to standardise on using URL-standard forward slashes. This will help you make your project platform agnostic.
One of the more powerful features of Platform.VirtualFileSystem is to create new file systems based on a directory on another file system. Use the IDirectory.CreateView method.
public static void Main()
{
var windowsDirectory = FileSystemManager.Default.ResolveDirectory("file://C:/Windows");
var windowsFileSystem = windowsDirectory.CreateView("windows");
}
The file system returned by CreateView now has C:\Windows as its root (though this is hidden from any users of the IFileSystem). If you want the file system to be resolvable globally you can add the newly created file system to the default FileSystemManager using the AddFileSystem method.
public static void Main()
{
var windowsDirectory = FileSystemManager.Default.ResolveDirectory("file://C:/Windows");
var windowsFileSystem = windowsDirectory.CreateView("windows");
FileSystemManager.Default.AddFileSystem(windowsFileSystem);
}
Instead of creating and registering views programmatically, you can declare custom views (and FileSystemManagers) inside your app.config. See the example app.config from the tests project.
Now you can resolve any files in the windows directory using the windows:// scheme:
public static void Main()
{
var windowsDirectory = FileSystemManager.Default.ResolveDirectory("file://C:/Windows");
var windowsFileSystem = windowsDirectory.CreateView("windows");
FileSystemManager.Default.AddFileSystem(windowsFileSystem);
var system32Directory = FileSystemManager.Default.ResolveDirectory("windows:///System32");
}
Now try grabbing all the directories within the directory using IDirectory.GetFiles().
public static void Main()
{
...
var system32Directory = FileSystemManager.Default.ResolveDirectory("windows:///System32");
foreach (var child in system32Directory.GetFiles())
{
Console.WriteLine(child.Address.AbsolutePath);
}
}
You can pass in a Predicate to GetFiles to filter the files you want. For example, you can ask for only files that have a dll extension. You could also use the Linq Select to accomplish the same thing but by passing the predicates directly into GetFiles you will get some things for free (such as the ability to do server side filtering when using the inbuilt network file system support).
public static void Main()
{
...
var system32Directory = FileSystemManager.Default.ResolveDirectory("windows:///System32");
foreach (var child in system32Directory.GetFiles(c => c.Address.Extension == "dll"))
{
Console.WriteLine(child.Address.AbsolutePath);
}
}
Every INode extends INodeResolver which means you can resolve new nodes relatively from any file or directory. You can easily resolve a file relative to system32Directory and read its contents.
public static void Main()
{
...
var file = system32Directory.ResolveFile("drivers/etc/hosts");
using(var reader = file.GetContent().GetReader())
{
Console.WriteLine(reader.ReadToEnd());
}
}
You can retrieve the parent directory of a file using the ParentDirectory property or by resolving "..". You can even resolve directories using more complex paths.
public static void Main()
{
...
/*** Get the parent directory of "System32" ***/
// Resolve using ParentDirectory property
var parentDirectoryMethodA = system32Directory.ParentDirectory;
// Resolve using relative path
var parentDirectoryMethodB = system32Directory.ResolveDirectory("..");
/*** Get the "windows://help" directory (physically C:/Windows/Help) ***/
// Resolve using relative path from system32
var windowsHelpDirectoryMethodA = system32Directory.ResolveDirectory("../Help");
// Resolve using over-the-top relative path from system32
var windowsHelpDirectoryMethodA = system32Directory.ResolveDirectory(".././Help/../Help/../Help/.");
// Resolve via the ParentDirectory property from system32
var windowsHelpDirectoryMethodB = system32Directory.ParentDirectory.ResolveDirectory("Help");
// Resolve using an absolute path relative to system32directory
var windowsHelpDirectoryMethodC = system32Directory.ResolveDirectory("/System32/Help");
// Resolve using an absolute path relative to windows file system
var windowsHelpDirectoryMethodC = system32Directory.FileSystem.ResolveDirectory("/System32/Help");
}