Click or drag to resize

IFolder Interface

IT Hit User File System
Represents a folder in the user file system.

Namespace:  ITHit.FileSystem
Assembly:  ITHit.FileSystem (in ITHit.FileSystem.dll) Version: 3.3.9602.0
Syntax
public interface IFolder : IFileSystemItem

The IFolder type exposes the following members.

Methods
  NameDescription
Public methodCreateFileAsync
Creates a new file in this folder in the remote storage.
Public methodCreateFolderAsync
Creates a new folder in the remote storage.
Public methodCode exampleDeleteAsync
Called when a file or a folder is being deleted from the user file system.
(Inherited from IFileSystemItem.)
Public methodDeleteCompletionAsync
Called when the delete operation is completed.
(Inherited from IFileSystemItem.)
Public methodGetChildrenAsync
Gets list of files and folders contained in this folder that match a search pattern.
Public methodGetMetadataAsync
Gets information about a file or a folder in a user file system, such as name, creation and modification date, etc.
(Inherited from IFileSystemItem.)
Public methodCode exampleMoveToAsync
Called when a file or a folder is being moved to a new location or renamed.
(Inherited from IFileSystemItem.)
Public methodMoveToCompletionAsync
Called when a file or a folder move or rename operation is completed.
(Inherited from IFileSystemItem.)
Public methodWriteAsync
Updates this folder info in the remote storage.
Top
Remarks

In addition to metods provided by IFileSystemItem provides GetChildrenAsync(String, IOperationContext, IFolderListingResultContext) method for enumerating this folder content as well as CreateFileAsync(IFileMetadata, Stream) and CreateFolderAsync(IFolderMetadata) methods for creating new items in this folder.

Examples

The code below is part of 'VirtualFileSystem' C# sample provided with the SDK.

C#
internal class VfsFolder : VfsFileSystemItem, IFolder
{
    public VfsFolder(string path, ILogger logger, VfsEngine engine) : base(path, logger, engine)
    {

    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"IFolder.GetChildrenAsync({pattern})", UserFileSystemPath);

        IEnumerable<FileSystemItemBasicInfo> children = await new UserFolder(UserFileSystemPath).EnumerateChildrenAsync(pattern);

        // Filtering existing files/folders. This is only required to avoid extra errors in the log.
        List<IFileSystemItemBasicInfo> newChildren = new List<IFileSystemItemBasicInfo>();
        foreach (IFileSystemItemBasicInfo child in children)
        {
            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);
            if (!FsPath.Exists(userFileSystemItemPath))
            {
                Logger.LogMessage("Creating", child.Name);
                newChildren.Add(child);
            }
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        resultContext.ReturnChildren(newChildren.ToArray(), newChildren.Count());


        // Create ETags.
        // ETags must correspond with a server file/folder, NOT with a client placeholder. 
        // It should NOT be moved/deleted/updated when a placeholder in the user file system is moved/deleted/updated.
        // It should be moved/deleted when a file/folder in the remote storage is moved/deleted.
        foreach (FileSystemItemBasicInfo child in children)
        {
            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);
            await ETag.SetETagAsync(userFileSystemItemPath, child.ETag);
        }
    }
}
C#
internal class VfsFolder : VfsFileSystemItem, IFolder
{
    public VfsFolder(string path, ILogger logger, VfsEngine engine) : base(path, logger, engine)
    {

    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"IFolder.GetChildrenAsync({pattern})", UserFileSystemPath);

        IEnumerable<FileSystemItemBasicInfo> children = await new UserFolder(UserFileSystemPath).EnumerateChildrenAsync(pattern);

        // Filtering existing files/folders. This is only required to avoid extra errors in the log.
        List<IFileSystemItemBasicInfo> newChildren = new List<IFileSystemItemBasicInfo>();
        foreach (IFileSystemItemBasicInfo child in children)
        {
            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);
            if (!FsPath.Exists(userFileSystemItemPath))
            {
                Logger.LogMessage("Creating", child.Name);
                newChildren.Add(child);
            }
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        resultContext.ReturnChildren(newChildren.ToArray(), newChildren.Count());


        // Save ETags and set "locked by another user" icon.
        foreach (FileSystemItemBasicInfo child in children)
        {
            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, child.Name);

            // Create ETags.
            // ETags must correspond with a server file/folder, NOT with a client placeholder. 
            // It should NOT be moved/deleted/updated when a placeholder in the user file system is moved/deleted/updated.
            // It should be moved/deleted when a file/folder in the remote storage is moved/deleted.
            ETag.SetETagAsync(userFileSystemItemPath, child.ETag);

            // Set the lock icon and read-only attribute, to indicate that the item is locked by another user.
            new UserFileSystemRawItem(userFileSystemItemPath).SetLockedByAnotherUserAsync(child.LockedByAnotherUser);
        }
    }
}
C#
public class VirtualFolder : VirtualFileSystemItem, IFolder
{
    public VirtualFolder(string path, ILogger logger) : base(path, logger)
    {

    }

    public async Task CreateFileAsync(IFileMetadata fileMetadata, Stream content = null)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", Path.Combine(UserFileSystemPath, fileMetadata.Name));

        FileInfo remoteStorageItem = new FileInfo(Path.Combine(RemoteStoragePath, fileMetadata.Name));

        // Upload remote storage file content.
        await using (FileStream remoteStorageStream = remoteStorageItem.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Delete))
        {
            if (content != null)
            {
                await content.CopyToAsync(remoteStorageStream);
                remoteStorageStream.SetLength(content.Length);
            }
        }

        // Update remote storage file metadata.
        remoteStorageItem.Attributes = fileMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = fileMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = fileMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;
    }

    public async Task CreateFolderAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", Path.Combine(UserFileSystemPath, folderMetadata.Name));

        DirectoryInfo remoteStorageItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, folderMetadata.Name));
        remoteStorageItem.Create();

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"{nameof(IFolder)}.{nameof(GetChildrenAsync)}({pattern})", UserFileSystemPath);

        IEnumerable<FileSystemInfo> remoteStorageChildren = new DirectoryInfo(RemoteStoragePath).EnumerateFileSystemInfos(pattern);

        List<IFileSystemItemMetadata> userFileSystemChildren = new List<IFileSystemItemMetadata>();
        foreach (FileSystemInfo remoteStorageItem in remoteStorageChildren)
        {
            IFileSystemItemMetadata itemInfo = Mapping.GetUserFileSysteItemMetadata(remoteStorageItem);
            userFileSystemChildren.Add(itemInfo);

            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, itemInfo.Name);

            // Filtering existing files/folders. This is only required to avoid extra errors in the log.
            if (!FsPath.Exists(userFileSystemItemPath))
            {
                Logger.LogMessage("Creating", userFileSystemItemPath);
                userFileSystemChildren.Add(itemInfo);
            }
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        resultContext.ReturnChildren(userFileSystemChildren.ToArray(), userFileSystemChildren.Count());
    }

    public async Task WriteAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath);

        DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath);

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
    }
}
C#
public class VirtualFolder : VirtualFileSystemItem, IFolder
{
    public VirtualFolder(string path, ILogger logger) : base(path, logger)
    {

    }

    public async Task<string> CreateFileAsync(IFileMetadata fileMetadata, Stream content = null)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", Path.Combine(UserFileSystemPath, fileMetadata.Name));

        FileInfo remoteStorageItem = new FileInfo(Path.Combine(RemoteStoragePath, fileMetadata.Name));

        // Upload remote storage file content.
        await using (FileStream remoteStorageStream = remoteStorageItem.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Delete))
        {
            if (content != null)
            {
                await content.CopyToAsync(remoteStorageStream);
                remoteStorageStream.SetLength(content.Length);
            }
        }

        // Update remote storage file metadata.
        remoteStorageItem.Attributes = fileMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = fileMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = fileMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;

        // Return remote storage item ID.
        return remoteStorageItem.FullName; 
    }

    public async Task<string> CreateFolderAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", Path.Combine(UserFileSystemPath, folderMetadata.Name));

        DirectoryInfo remoteStorageItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, folderMetadata.Name));
        remoteStorageItem.Create();

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;

        // Return remote storage item ID.
        return remoteStorageItem.FullName;
    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"{nameof(IFolder)}.{nameof(GetChildrenAsync)}({pattern})", UserFileSystemPath);

        IEnumerable<FileSystemInfo> remoteStorageChildren = new DirectoryInfo(RemoteStoragePath).EnumerateFileSystemInfos(pattern);

        List<IFileSystemItemMetadata> userFileSystemChildren = new List<IFileSystemItemMetadata>();
        foreach (FileSystemInfo remoteStorageItem in remoteStorageChildren)
        {
            IFileSystemItemMetadata itemInfo = Mapping.GetUserFileSysteItemMetadata(remoteStorageItem);

            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, itemInfo.Name);

            // Filtering existing files/folders. This is only required to avoid extra errors in the log.
            if (!FsPath.Exists(userFileSystemItemPath))
            {
                Logger.LogMessage("Creating", userFileSystemItemPath);
                userFileSystemChildren.Add(itemInfo);
            }
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        resultContext.ReturnChildren(userFileSystemChildren.ToArray(), userFileSystemChildren.Count());
    }

    public async Task WriteAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath);

        DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath);

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
    }
}
C#
public class VirtualFolder : VirtualFileSystemItem, IFolder
{
    public VirtualFolder(string path, byte[] itemId, ILogger logger) : base(path, itemId, logger)
    {

    }

    public async Task<byte[]> CreateFileAsync(IFileMetadata fileMetadata, Stream content = null)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", Path.Combine(UserFileSystemPath, fileMetadata.Name));

        FileInfo remoteStorageItem = new FileInfo(Path.Combine(RemoteStoragePath, fileMetadata.Name));

        // Upload remote storage file content.
        await using (FileStream remoteStorageStream = remoteStorageItem.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Delete))
        {
            if (content != null)
            {
                await content.CopyToAsync(remoteStorageStream);
                remoteStorageStream.SetLength(content.Length);
            }
        }

        // Update remote storage file metadata.
        remoteStorageItem.Attributes = fileMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = fileMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = fileMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = fileMetadata.LastWriteTime.UtcDateTime;

        // Return remote storage item ID. It will be passed later into IEngine.GetFileSystemItemAsync() method.
        return WindowsFileSystemItem.GetItemIdByPath(remoteStorageItem.FullName); 
    }

    public async Task<byte[]> CreateFolderAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", Path.Combine(UserFileSystemPath, folderMetadata.Name));

        DirectoryInfo remoteStorageItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, folderMetadata.Name));
        remoteStorageItem.Create();

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;

        // Return remote storage item ID. It will be passed later into IEngine.GetFileSystemItemAsync() method.
        return WindowsFileSystemItem.GetItemIdByPath(remoteStorageItem.FullName);
    }

    public async Task GetChildrenAsync(string pattern, IOperationContext operationContext, IFolderListingResultContext resultContext)
    {
        // This method has a 60 sec timeout. 
        // To process longer requests and reset the timout timer call one of the following:
        // - resultContext.ReturnChildren() method.
        // - resultContext.ReportProgress() method.

        Logger.LogMessage($"{nameof(IFolder)}.{nameof(GetChildrenAsync)}({pattern})", UserFileSystemPath);

        IEnumerable<FileSystemInfo> remoteStorageChildren = new DirectoryInfo(RemoteStoragePath).EnumerateFileSystemInfos(pattern);

        List<IFileSystemItemMetadata> userFileSystemChildren = new List<IFileSystemItemMetadata>();
        foreach (FileSystemInfo remoteStorageItem in remoteStorageChildren)
        {
            IFileSystemItemMetadata itemInfo = Mapping.GetUserFileSysteItemMetadata(remoteStorageItem);

            string userFileSystemItemPath = Path.Combine(UserFileSystemPath, itemInfo.Name);

            // Filtering existing files/folders. This is only required to avoid extra errors in the log.
            if (!FsPath.Exists(userFileSystemItemPath))
            {
                Logger.LogMessage("Creating", userFileSystemItemPath);
                userFileSystemChildren.Add(itemInfo);
            }
        }

        // To signal that the children enumeration is completed 
        // always call ReturnChildren(), even if the folder is empty.
        resultContext.ReturnChildren(userFileSystemChildren.ToArray(), userFileSystemChildren.Count());
    }

    public async Task WriteAsync(IFolderMetadata folderMetadata)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath);

        DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath);

        // Update remote storage folder metadata.
        remoteStorageItem.Attributes = folderMetadata.Attributes;
        remoteStorageItem.CreationTimeUtc = folderMetadata.CreationTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
        remoteStorageItem.LastAccessTimeUtc = folderMetadata.LastAccessTime.UtcDateTime;
        remoteStorageItem.LastWriteTimeUtc = folderMetadata.LastWriteTime.UtcDateTime;
    }
}
See Also