To implement your own Virtual File System, I suggest you to implement your own custom File System Provider. You can find inspiration at FileServerCustomFS sample, which contains two custom File System Providers.
If you still rather prefer event based API, you need to rewrite also:
GetNodeSurrogate
- to resolve your custom nodes.
GetContentSurrogate
- to provide your custom file content.
GetLengthSurrogate
- to display correct content length.
Please note that items assigned in the GetChildrenSurrogate
are not stored anywhere. If you want to keep them and use them later, you have to do it on our own.
The sample implementation using notifier can look like this:
private class MyDirectory : DirectoryNode
{
public IEnumerable<NodeBase> Children { get; set; }
public MyDirectory(string nodeName, DirectoryNode parent, NodeTimeInfo nodeTimeInfo = null, NodeAttributes attributes = null) : base(nodeName, parent, nodeTimeInfo, attributes)
{
}
}
private class MyFile : FileNode
{
public MyFile(string nodeName, DirectoryNode parent, NodeTimeInfo nodeTimeInfo = null, NodeAttributes attributes = null) : base(nodeName, parent, nodeTimeInfo, attributes)
{
}
public long GetLength()
{
return this.Path.StringPath.Length;
}
public NodeContent GetReadContent()
{
// generate file content on-the-fly or read it from DB
var ms = new MemoryStream(Encoding.ASCII.GetBytes(this.Path.ToString()));
return NodeContent.CreateReadOnlyContent(ms);
// or open associated file on disk
return NodeContent.CreateReadOnlyContent(File.OpenRead("..." + this.Path));
}
}
private static IEnumerable<NodeBase> GenerateChildren(DirectoryNode parent, Dictionary<string, NodeBase> nodes)
{
// generate some children for this node
var d = new MyDirectory("level-" + parent.Path.PathPartsCount, parent);
var f = new MyFile("level-" + parent.Path.PathPartsCount + ".txt", parent);
// add them to global list
nodes[d.Path.StringPath] = d;
nodes[f.Path.StringPath] = f;
// return as collection
var children = new List<NodeBase>();
children.Add(d);
children.Add(f);
return children;
}
...
var nodes = new Dictionary<string, NodeBase>(StringComparer.OrdinalIgnoreCase);
var vfs = new MountCapableFileSystemProvider();
var notifier = vfs.GetFileSystemNotifier();
notifier.GetChildrenSurrogate += (s, e) =>
{
if (e.Node.IsRootDirectory)
{
if (e.Node.Context == null)
e.Node.Context = GenerateChildren(e.Node as DirectoryNode, nodes);
e.ResultChildren = e.Node.Context as IEnumerable<NodeBase>;
return;
}
var mydir = e.Node as MyDirectory;
if (mydir == null)
return;
if (mydir.Children == null)
mydir.Children = GenerateChildren(mydir, nodes);
e.ResultChildren = mydir.Children;
};
notifier.GetNodeSurrogate += (s, e) =>
{
NodeBase node;
if (nodes.TryGetValue(e.Path.StringPath, out node))
e.ResultNode = node;
};
notifier.GetContentSurrogate += (s, e) =>
{
var myfile = e.Node as MyFile;
if (myfile == null)
return;
e.ResultContent = myfile.GetReadContent();
};
notifier.GetLengthSurrogate += (s, e) =>
{
var myfile = e.Node as MyFile;
if (myfile == null)
return;
e.ResultLength = myfile.GetLength();
};