• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IContentAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • Dependency Injection
        • AccessorMessages
    • Avalanche.Binding
      • Introduction
    • Avalanche.Core
      • License
    • Avalanche.DataType
      • Introduction
      • DataType
        • IDataType
        • IListType
        • IMapType
        • IRecordType
        • IFieldType
        • IOneOfType
        • IAnyType
        • IStringType
        • IValueType
        • IIntegerType
        • IEnumerationType
        • IRealType
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • Enum
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
      • Asp.Net
        • Introduction
        • Supplying localization
        • Inject to pages
        • Culture Assigned
        • Minimalistic Api
        • Diagnostics
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR40
        • Unicode.CLDR41
        • Unicode.CLDR42
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Logging
        • Validation
    • Avalanche.Service
      • Introduction
      • Service
        • Introduction
        • IService
        • IServiceDisposable
        • IServiceDecoration
        • IServiceCast
        • IServiceObservable
        • IServiceContainer
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Scope
      • Handler
        • Introduction
        • IHandler
        • IHandlerCast
        • IHandlerDecoration
        • IHandlerWithOrder
        • CancellationToken
        • Cyclicity
        • Delegates
        • Invokable
        • ExportAttribute
        • OrderAttribute
        • PrintTree
        • Recursion
      • Query
        • Introduction
        • IQuery
        • IQueryCast
        • IQueryDecoration
      • Entry
        • Introduction
        • IEntry
        • IEntryCast
        • IEntryDecoration
        • IEntryObservable
        • IEntryVisitable
        • EntryState
      • Request
        • Introduction
        • IRequest
        • IRequestFor
        • IRequestToBeCached
        • IRequestToBeDisposed
        • RequestAttribute
        • ContextParameterAttribute
        • Print Tree
      • Dependency Injection
        • Introduction
        • Asp.Net
        • ServiceRequest<T>
        • Decorating a service
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
      • HttpStatusCode
      • OpcUaStatusCode
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • ListCloner
        • DictionaryCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Pipe
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
        • IProviderEvent
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    FileSystem

    new FileSystem(path) creates an instance of filesystem at path.

    IFileSystem fs = new FileSystem(@"C:\Temp\");
    

    .Browse(path) returns a snapshot of directory contents.

    IDirectoryContent contents = fs.Browse("C:/Windows/");
    

    IDirectoryContent is enumerable IEnumerable<IEntry>.

    foreach (IEntry entry in fs.Browse("C:/Windows/"))
        Console.WriteLine(entry.Path);
    

    .AssertExists() asserts that directory exists. It throws DirectoryNotFound if not found.

    foreach (var entry in fs.Browse("C:/Windows/").AssertExists())
        Console.WriteLine(entry.Path);
    

    .GetEntry(path) reads a single file or directory entry. Returns null if entry is not found.

    IEntry e = FileSystem.OS.GetEntry("C:/Windows/win.ini")!;
    Console.WriteLine(e.Path);
    

    .AssertExists() asserts that null is not returned. Throws FileNotFoundException if entry was not found.

    IEntry e = FileSystem.OS.GetEntry("C:/Windows/win.ini")!.AssertExists();
    

    Files can be opened for reading.

    using (Stream s = fs.Open("file.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        Console.WriteLine(s.Length);
    }
    

    And for for writing.

    using (Stream s = fs.Open("somefile.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        s.WriteByte(32);
    }
    

    Directories can be created.

    fs.CreateDirectory("dir/");
    

    Directories can be deleted.

    fs.Delete("dir/", recurse: true);
    

    Files and directories can be renamed and moved.

    fs.CreateDirectory("dir/");
    fs.Move("dir/", "new-name/");
    

    And file attributes changed.

    fs.SetFileAttribute("myfile", FileAttributes.ReadOnly);
    

    Singleton

    The singleton instance FileSystem.OS refers to a filesystem at the OS root.

    IFileSystem fs = FileSystem.OS;
    

    Extension method .VisitTree() visits filesystem. On root path "" FileSystem.OS returns drive letters.

    foreach (var line in FileSystem.OS.VisitTree(depth: 2))
        Console.WriteLine(line);
    
    ""
    ├──"C:"
    │  ├── "hiberfil.sys"
    │  ├── "pagefile.sys"
    │  ├── "swapfile.sys"
    │  ├── "Documents and Settings"
    │  ├── "Program Files"
    │  ├── "Program Files (x86)"
    │  ├── "System Volume Information"
    │  ├── "Users"
    │  └── "Windows10"
    └──"D:"
    
    Note

    The separator character is always forward slash '/'. For example "C:/Windows/win.ini".

    Extension method .PrintTreeTo() appends the visited filesystem to text output.

    FileSystem.OS.PrintTreeTo(Console.Out, depth: 2, format: FileSystemPrintTreeExtensions.Format.DefaultPath);
    
    ├── C:/
    │  ├── C:/hiberfil.sys
    │  ├── C:/pagefile.sys
    │  ├── C:/swapfile.sys
    │  ├── C:/Documents and Settings/
    │  ├── C:/Program Files/
    │  ├── C:/Program Files (x86)/
    │  ├── C:/System Volume Information/
    │  ├── C:/Users/
    │  └── C:/Windows/
    └── D:/
    

    On linux FileSystem.OS returns slash '/' root.

    FileSystem.OS.PrintTreeTo(Console.Out, depth: 3, format: FileSystemPrintTreeExtensions.Format.DefaultPath);
    
    
    └──/
       ├──/bin/
       ├──/boot/
       ├──/dev/
       ├──/etc/
       ├──/lib/
       ├──/media/
       ├──/mnt/
       ├──/root/
       ├──/sys/
       ├──/usr/
       └──/var/
    

    FileSystem.Application refers to the application's root directory.

    FileSystem.Application.PrintTreeTo(Console.Out);
    
    ""
    ├── "Application.dll"
    ├── "Application.runtimeconfig.json"
    ├── "Avalanche.FileSystem.Abstractions.dll"
    └── "Avalanche.FileSystem.dll"
    

    FileSystem.Temp refers to the running user's temp directory.

    FileSystem.Temp.PrintTreeTo(Console.Out, depth: 1);
    
    ""
    ├── "dmk55ohj.jjp"
    ├── "wrz4cms5.r2f"
    └── "18e1904137f065db88dfbd23609eb877"
    

    Singleton instances:

    Name Description On Windows On Linux
    FileSystem.OS Operating system root. "" ""
    FileSystem.Application Running application's base directory.
    FileSystem.UserProfile The user's profile folder. "C:\Users\<user>" "/home/<user>"
    FileSystem.MyDocuments The My Documents folder. "C:\Users\<user>\Documents" "/home/<user>"
    FileSystem.Personal A common repository for documents. "C:\Users\<user>\Documents" "/home/<user>"
    FileSystem.Temp Running user's temp directory. "C:\Users\<user>\AppData\Local\Temp" "/tmp
    FileSystem.Config User's cloud-sync program configuration (roaming data). "C:\Users\<user>\AppData\Roaming" "/home/<user>/.config"
    FileSystem.Data User's local program data. "C:\Users\<user>\AppData\Local" "/home/<user>/.local/share"
    FileSystem.ProgramData Program data that is shared with every user. "C:\ProgramData" "/usr/share"
    FileSystem.Desktop User's desktop. "C:\Users\<user>\Desktop" "/home/user/Desktop"
    FileSystem.MyPictures User's pictures. "C:\Users\<user>\Pictures" "/home/user/Pictures"
    FileSystem.MyVideos User's videos. "C:\Users\<user>\Videos" "/home/user/Videos"
    FileSystem.MyMusic User's music. "C:\Users\<user>\Music" "/home/user/Music"
    FileSystem.Templates Templates. "C:\Users\<user>\AppData\Roaming\Microsoft\Windows\Templates" "/home/user/Templates"

    IFileEntry.PhysicalPath() returns physical path of file entry.

    foreach (var line in FileSystem.Temp.VisitTree(depth: 2))
        Console.WriteLine(line.Entry.PhysicalPath());
    

    TreeVisitor prints physical path with PrintTree.Format.PhysicalPath flag.

    FileSystem.Temp.PrintTreeTo(
        output: Console.Out,
        depth: 2,
        format: FileSystemPrintTreeExtensions.Format.Default | FileSystemPrintTreeExtensions.Format.PhysicalPath);
    
    "" [C:\Users\\user\\AppData\Local\Temp\]
    ├── "dmk55ohj.jjp" [C:\Users\\user\\AppData\Local\Temp\dmk55ohj.jjp]
    ├── "wrz4cms5.r2f" [C:\Users\\user\\AppData\Local\Temp\wrz4cms5.r2f]
    └── "18e1904137f065db88dfbd23609eb877" [C:\Users\\user\\AppData\Local\Temp\18e1904137f065db88dfbd23609eb877]
    

    Observing

    Files and directories can be observed for changes.

    IObserver<Avalanche.FileSystem.IEvent> observer = new Observer();
    IFileSystemObserver handle = FileSystem.OS.Observe("C:/**", observer);
    

    Observer can be used in a using scope.

    using (var handle = FileSystem.Temp.Observe("*.dat", new PrintObserver()))
    {
        FileSystem.Temp.CreateFile("file.dat", new byte[] { 32, 32, 32, 32 });
        FileSystem.Temp.Delete("file.dat");
    
        Thread.Sleep(1000);
    }
    
    class PrintObserver : IObserver<Avalanche.FileSystem.IEvent>
    {
        public void OnCompleted() => Console.WriteLine("OnCompleted");
        public void OnError(Exception error) => Console.WriteLine(error);
        public void OnNext(Avalanche.FileSystem.IEvent @event) => Console.WriteLine(@event);
    }
    
    StartEvent(C:\Users\\<i>&lt;user&gt;</i>\\AppData\Local\Temp\, 23.10.2019 16.27.01 +00:00)
    CreateEvent(C:\Users\\<i>&lt;user&gt;</i>\\AppData\Local\Temp\, 23.10.2019 16.27.01 +00:00, file.dat)
    ChangeEvent(C:\Users\\<i>&lt;user&gt;</i>\\AppData\Local\Temp\, 23.10.2019 16.27.01 +00:00, file.dat)
    DeleteEvent(C:\Users\\<i>&lt;user&gt;</i>\\AppData\Local\Temp\, 23.10.2019 16.27.01 +00:00, file.dat)
    OnCompleted
    

    Disposing

    Disposable objects can be attached to be disposed along with FileSystem.

    // Init
    object obj = new ReaderWriterLockSlim();
    IFileSystemDisposable fs = new FileSystem("").AttachDisposable(obj);
    
    // ... do work ...
    
    // Dispose both
    fs.Dispose();
    

    Delegates can be attached to be executed at dispose of FileSystem.

    IFileSystemDisposable fs = new FileSystem("")
        .AddDisposeAction(f => Console.WriteLine("Disposed"));
    

    .BelateDispose() creates a handle that postpones dispose on .Dispose(). Actual dispose will proceed once .Dispose() is called and all belate handles are disposed. This can be used for passing the IFileSystem to a worker thread.

    FileSystem fs = new FileSystem("");
    fs.Browse("");
    
    // Postpone dispose
    IDisposable? belateDisposeHandle = fs.BelateDispose();
    // Start concurrent work
    Task.Run(() =>
    {
        // Do work
        Thread.Sleep(1000);
        fs.GetEntry("");
        // Release belate handle. Disposes here or below, depending which thread runs last.
        belateDisposeHandle?.Dispose();
    });
    
    // Start dispose, but postpone it until belatehandle is disposed in another thread.
    fs.Dispose();
    
    In This Article
    Back to top Copyright © Toni Kalajainen