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: 8.4.27341.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 before a file or a folder is being deleted from the user file system.
(Inherited from IFileSystemItem.)
Public methodGetChildrenAsync
Gets list of files and folders contained in this folder that match a search pattern.
Public methodCode exampleGetPropertiesAsync
Gets list of custom properties.
(Inherited from IFileSystemItem.)
Public methodCode exampleGetThumbnailAsync
Called when thumbnail is requested by the platform.
(Inherited from IFileSystemItem.)
Public methodCode exampleMoveToAsync
Called before a file or a folder is moved to a new location or renamed.
(Inherited from IFileSystemItem.)
Public methodWriteAsync
Updates this folder info in the remote storage.
Top
Remarks
Examples

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

C#
public class VirtualFolder : VirtualFileSystemItem, IFolder
{
    public VirtualFolder(IMapping mapping, string path, ILogger logger) : base(mapping, path, logger)
    {

    }

    public async Task<IFileMetadata> CreateFileAsync(IFileMetadata metadata, Stream content = null, IOperationContext operationContext = null, IInSyncResultContext inSyncResultContext = null, CancellationToken cancellationToken = default)
    {
        string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, metadata.Name);
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}()", userFileSystemNewItemPath, default, operationContext, metadata);

        FileInfo remoteStorageNewItem = new FileInfo(Path.Combine(RemoteStoragePath, metadata.Name));

        // Create remote storage file.
        using (FileStream remoteStorageStream = remoteStorageNewItem.Open(FileMode.CreateNew, FileAccess.Write, FileShare.Delete))
        {
            // Upload content. Note that if the file is blocked - content parameter is null.
            if (content != null)
            {
                try
                {
                    await content.CopyToAsync(remoteStorageStream);
                }
                catch (OperationCanceledException)
                {
                    // Operation was canceled by the calling Engine.StopAsync() or the operation timeout occurred.
                    Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFileAsync)}() canceled", userFileSystemNewItemPath, default, operationContext, metadata);
                }
                remoteStorageStream.SetLength(content.Length);
            }
        }

        // Update remote storage file metadata.
        remoteStorageNewItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly;
        remoteStorageNewItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime;
        remoteStorageNewItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime;
        remoteStorageNewItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime;
        remoteStorageNewItem.Attributes = metadata.Attributes.Value;

        // Typically you must return IFileMetadata with a remote storage item ID, content eTag and metadata eTag.
        // The ID will be passed later into IEngine.GetFileSystemItemAsync() method.
        // However, becuse we can not read the ID for the network path we return null.
        return null; 
    }

    public async Task<IFolderMetadata> CreateFolderAsync(IFolderMetadata metadata, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default)
    {
        string userFileSystemNewItemPath = Path.Combine(UserFileSystemPath, metadata.Name);
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(CreateFolderAsync)}()", userFileSystemNewItemPath, default, operationContext, metadata);

        DirectoryInfo remoteStorageNewItem = new DirectoryInfo(Path.Combine(RemoteStoragePath, metadata.Name));
        remoteStorageNewItem.Create();

        // Update remote storage folder metadata.
        remoteStorageNewItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly;
        remoteStorageNewItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime;
        remoteStorageNewItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime;
        remoteStorageNewItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime;
        remoteStorageNewItem.Attributes = metadata.Attributes.Value;

        // Typically you must return IFileMetadata with a remote storage item ID and metadata eTag.
        // The ID will be passed later into IEngine.GetFileSystemItemAsync() method.
        // However, becuse we can not read the ID for the network path we return null.
        return null;
    }

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

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

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

        foreach (FileSystemInfo remoteStorageItem in remoteStorageChildren)
        {
            IFileSystemItemMetadata itemInfo = Mapping.GetUserFileSysteItemMetadata(remoteStorageItem);
            children.Add(itemInfo);
        }

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

    public async Task<IFolderMetadata> WriteAsync(IFolderMetadata metadata, IOperationContext operationContext, IInSyncResultContext inSyncResultContext, CancellationToken cancellationToken = default)
    {
        Logger.LogMessage($"{nameof(IFolder)}.{nameof(WriteAsync)}()", UserFileSystemPath, default, operationContext, metadata);

        DirectoryInfo remoteStorageItem = new DirectoryInfo(RemoteStoragePath);

        // Update remote storage folder metadata.
        if (metadata.Attributes.HasValue)
        {
            remoteStorageItem.Attributes = metadata.Attributes.Value & ~FileAttributes.ReadOnly;
        }

        if (metadata.CreationTime.HasValue)
        {
            remoteStorageItem.CreationTimeUtc = metadata.CreationTime.Value.UtcDateTime;
        }

        if (metadata.LastWriteTime.HasValue)
        {
            remoteStorageItem.LastWriteTimeUtc = metadata.LastWriteTime.Value.UtcDateTime;
        }

        if (metadata.LastAccessTime.HasValue)
        {
            remoteStorageItem.LastAccessTimeUtc = metadata.LastAccessTime.Value.UtcDateTime;
        }

        if (metadata.Attributes.HasValue)
        {
            remoteStorageItem.Attributes = metadata.Attributes.Value;
        }

        return null;
    }
}
See Also