https://github.com/FrozenCow/sharpfilesystem
SharpFileSystem is a Virtual File System (VFS) implementation for .NET to allow access to different filesystems in the same way for normal files and directories.
After looking a long time for a VFS for .NET, so that I could read files and directories in archives (zip and rar) the same way as any ordinary files and directories. I couldn't find any complete solution for this problem, so I decided to implement a VFS myself. Also what I didn't like in the ordinary filesystems was the path-system. It allowed relative paths (like ..), which can often lead to security issues without explicit checking. It allows referring to directories the same way as referring to files. This often leads to strange behavior, like copying a directory (source) to another directory (destination), here it is often vague whether the destination directory should be overwritten or if it should copy the source-directory inside the destination-directory.
At the moment the following filesystems are implemented:
There are also filesystems that alter the exposed structure of existing filesystems. These allow you to mix different systems together to get the desired file-structure:
At the heart of the system there is the IFileSystem
interface. This describes the operations that should be provided by every filesystem:
public interface IFileSystem : IDisposable
{
ICollection GetEntities(FileSystemPath path);
bool Exists(FileSystemPath path);
Stream CreateFile(FileSystemPath path);
Stream OpenFile(FileSystemPath path, FileAccess access);
void CreateDirectory(FileSystemPath path);
void Delete(FileSystemPath path);
}
Normally in .NET/Mono we refer to files by their path coded as a string. This sometimes leads to inconsistencies, which is why I've created the type FileSystemPath to encapsulate the path in SharpFileSystem. There are operations that help create and alter the path, but the rules of using them are more strict, which creates a much more robust environment for the programmer:
/
./
. This means: there are no relative paths./
./
.System.IO.Path.Combine
for .NET).Some examples of using FileSystemPath
:
Operation | Result |
---|---|
var root = FileSystemPath.Root |
/ |
var mydir = root.AppendDirectory("mydir") |
/mydir/ |
var myfile = mydir.AppendFile("myfile") |
/mydir/myfile |
myfile.AppendFile("myfile2") |
Error: The specified FileSystemPath is not a directory. |
mydir.ParentPath |
/ |
https://github.com/nreco/nicnet/tree/master/src/NI.Vfs
Nuget Package | NI.Vfs |
Source code | NI.Vfs on GitHub |
API Reference | NI.Vfs |
using NI.Vfs; ... IFileSystem localFs = new LocalFileSystem("c:\\temp"); var txtFile = localFs.ResolveFile("test1.txt"); txtFile.CreateFile();
https://github.com/platformdotnet/Platform.VirtualFileSystem
Read the Platform.VirtualFileSystem wiki
A virtual file system for C#, .NET and Mono.
Platform.VirtualFileSystem provides a uniform, cross-platform and managed abstraction layer for file systems. It is similar to VFS features of various operating systems like Linux except it all runs in managed code. Features include:
zip://[file:/C:/Test.zip]/a.txt
IFileHashingService
andIFileTransferService
). All services implementPlatform.ITask
meaning they can all run the background; are monitorable (percent complete, bytes transferred etc) and can be paused, or cancelled.INodeResolutionFilter
. Operations on files can be intercepted and/or overidden usingINodeOperationFilter
.IFile
and IDirectory
interfaces. app.config
or web.config
.Platform.VirtualFileSystem is available via nuget. You can search for Platform.VirtualFileSystem
using the Visual Studio NuGet plugin UI or by typingInstall-Package Platform.VirtualFileSystem
into the Visual Studio Package Manager Console. If you want zip support you should also install the zip provider usingInstall-Package Platform.VirtualFileSystem.Providers.Zip
.
A package for those who don't use NuGet will be available in the future. In the mean time you can download the latest zip of the NuGet package direct from thenuget website. Note that you will also need the Platform.NET package.
Three assemblies are required for Platform.VirtualFileSystem: Platform.dll
,Platform.Xml.Serialization
andPlatform.VirtualFileSystem.dll
. If you require zip support thenPlatform.VirtualFileSystem.Providers.Zip.dll
andICSharpCode.SharpZipLib
is also required. Platform.VirtualFileSystem will automatically load the zip provider if thePlatform.VirtualFileSystem.Providers.Zip.dll
is located in the same directory asPlatform.VirtualFileSystem.dll
.
Provides access to the concerete file sytems of the underlying operating system. Example URIs:
C:\Windows\Temp
file:///usr/local/src
file://c:/Windows/Temp
Provides read-only access to ftp and http file systems view the .NET HTTP WebRequest object. Example URIs:
http://www.github.com
Create custom file systems rooted from a directory of any other file system. This is very useful for avoiding the use of absolute concrete file system paths. For example, you can mapD:\App\Data
to a view accessible viaappdata://
. All your code will be resilient to any changes to the underlying location of theappdata://
view. You could moveD:\App\Data
to E:\App\Data
or even to a non-local provider without breaking any existing code. Views also add security by ensuring code can't access files above the root of the view. Example URIs:
config:///A/B.txt
mycustomview:///A/B.txt
Similar to the /proc
file system on Linux, the SystemInfo provider provides simple read-only access to environment variables and read-write access to the system clock. Access to more advanced operating system settings are planned for the future.
systeminfo:///EnvironmentVariables/TEMP
systeminfo:///SystemClock/CurrentDateTime
Create a file system made up of the merged view of one or more file systems.
overlayed://[file:///tmp/a;file:///tmp/b]/c
A provider accessible via the temp://
scheme that provides access to temporary storage (usually backed by/tmp
on Unix andC:\Windows\Temp
on Windows)
temp:///tempfile1.dat
temp:///tempfile2.dat
Provides a simple and cross-platform consistent view of the disks on a system.
mycomputer://C
mycomputer://D
mycomputer://C/Windows
mycomputer://CDROM-1/i386
mycomputer://FLOPPY-1/GAME/LEMMINGS
Provides a simple interface for load/saving of objects serialized into files (seeDataFileTests.cs
). RequiresPlatform.VirtualFileSystem.DataFile.dll
.
Exposes all VFS file systems over TCP. Many services are supported natively (for exampleIFileHashingService
would be executed remotely on the server). RequiredPlatform.VirtualFileSystem.Network.*.dll
.
netvfs://hostname[file:///usr//]/local/src
Provides a virtualised view of zip files. Both readonly and random readwrite support zip files has been supported since version 1.0.0.45. You can also directly create a zip file from an existing directory usingZipFileSystem.CreateZipFile
.
zip://[file:///home/tum/Test.zip]/Directory/Textfile.txt
// Get a directory
var localDir = FileSystemManager.Default.ResolveDirectory("C:/Windows");
// Create a view with the scheme 'windows'
var windowsFileSystem = localDir.CreateView("windows");
var system32 = windowsFileSystem.ResolveDirectory("System32");
// Will output: windows:///System32
Console.WriteLine(system32.Address);
// Add the file system to the default FileSystemManager
FileSystemManager.Default.AddFileSystem(windowsFileSystem);
var windowsNotepad = FileSystemManager.Default.ResolveFile("windows:///notepad.exe");
// Will output: True
Console.WriteLine(windowsNotepad.Exists);
var root = localDir.ResolveFile("..");
// Will output: file://C:/
Console.WriteLine(root.Address);
var localNotepad = dir.ResolveFile("notepad.exe");
// Will output: True
Console.WriteLine(localNotepad.Exists);
// Will output: /Windows/notepad.exe
Console.WriteLine(root.Address.GetRelativePathTo(localNotepad.Address));
// Will output: ../..
Console.WriteLine(root.Address.GetRelativePathTo(localNotepad.Address));
Refer to the Platform.VirtualFileSystem.Tests project for more examples.
The default file system manager (FileSystemManager.Default
) contains all the standard file system providers except for Network and Zip. If you want to custom define what is available or add views or custom providers, you can extend the IFileSystemManager interface, construct and populate a separate StandardFileSystemManager
or define file systems within yourapp.config
orweb.config
. Please reference app.config from the tests project to see an example of how to do the later.
Platform.VirtualFileSystem is released under the terms of the new BSD license as documentedhere. Zip support is provided by a separate assembly (Platform.VirtualFileSystem.Providers.Zip
) which depends onSharpZipLib
which is licensed under the GPL with an exception that permits commercial use.
Platform.VirtualFileSystem was written quite a few years ago and has been recently updated to modern C#. It was written in a time before type inference, automatic properties and lambda expressions so if you see legacy cruft please don't hesistate to update it.
Read the Platform.VirtualFileSystem wiki
http://vfs.codeplex.com/
//expose the contents of a given folder as a file system using a provider that operates on the local file system IFileSystemProvider provider = new LocalFileSystemProvider(@"C:\MyData"); //we can get the root through its qualified name or a dedicated method (recommended) VirtualFolderInfo root = provider.GetFileSystemRoot(); foreach (VirtualFileInfo file in provider.GetFiles(root)) { //access meta data Console.Out.WriteLine("Root folder contains file " + file.Name); //...read the file data (there's helper classes to deal with streams) Stream data = provider.ReadFileContents(file.FullName); //...or perform operations (delete, overwrite, move, copy) provider.DeleteFile(file.FullName); }
//get root folder VirtualFolder root= VirtualFolder.CreateRootFolder(provider); //create child folder on root level VirtualFolder childFolder = root.AddFolder("folder1"); //upload a local file's data to the FS - overwrite it if it already exists Stream stream = ... VirtualFile file = childFolder.AddFile("foo.txt", stream, true);
VirtualFolder root= VirtualFolder.CreateRootFolder(provider); foreach(VirtualFolder childFolder in root.GetFolders()) { foreach(VirtualFile file in childFolder.GetFiles("*.txt")) { VirtualFileInfo fileInfo = file.MetaData; Console.Out.WriteLine("Deleting file " + fileInfo.Name); file.Delete(); } }
//the provider will access the service as configured in app.config IFileSystemProvider provider = new RemoteServiceFileSystem(); //get root folder VirtualFolderInfo root = provider.GetFileSystemRoot(); //create two folders on the root level VirtualFolderInfo f1= provider.AddFolder(root, "folder1"); VirtualFolderInfo f2= provider.AddFolder(root, "folder2"); //copy the first folder into the second one (this operation will be performed on the server) provider.MoveFolder(f1, "/folder2/copy");
Last edited May 14, 2010 at 6:04 PM by phsumi, version 20