Click here to Skip to main content
12,243,058 members (50,208 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

254.4K views
4.2K downloads
113 bookmarked
Posted

Accessing alternative data-streams of files on an NTFS volume

, 28 Jul 2010 CPOL
A pair of classes to encapsulate access to NTFS alternative data streams.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using Trinet.Core.IO.Ntfs;

namespace NTFS
{
	public sealed class FileStreams : Collection<StreamInfo>
	{
		private readonly FileSystemInfo _file;
		
		public FileStreams(FileSystemInfo file)
		{
			if (null == file) throw new ArgumentNullException("file");
			
			_file = file;
			
			foreach (AlternateDataStreamInfo info in FileSystem.ListAlternateDataStreams(file))
			{
				Items.Add(new StreamInfo(info));
			}
		}
		
		public FileStreams(string path) : this(CreateFile(path))
		{
		}
		
		public FileSystemInfo FileInfo
		{
			get { return _file; }
		}
		
		public string FileName
		{
			get { return _file.FullName; }
		}
		
		public long FileSize
		{
			get
			{
				FileInfo file = _file as FileInfo;
				if (null != file) return file.Length;
				return 0L;
			}
		}
		
		public long Size
		{
			get
			{
				long value = this.FileSize;
				if (0 != this.Count)
				{
					foreach (StreamInfo info in this.Items)
					{
						value += info.Size;
					}
				}
				
				return value;
			}
		}
		
		public StreamInfo this[string streamName]
		{
			get
			{
				int index = this.IndexOf(streamName);
				if (-1 != index) return this.Items[index];
				return null;
			}
		}
		
		private static FileSystemInfo CreateFile(string path)
		{
			if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
			
			path = Path.GetFullPath(path);
			if (!File.Exists(path) && Directory.Exists(path)) return new DirectoryInfo(path);
			return new FileInfo(path);
		}
		
		public int IndexOf(string streamName)
		{
			int result = -1;
			if (0 != this.Count)
			{
				int index = 0;
				foreach (StreamInfo item in this.Items)
				{
					if (string.Equals(streamName, item.Name, StringComparison.OrdinalIgnoreCase))
					{
						result = index;
						break;
					}
					
					index++;
				}
			}
			
			return result;
		}
		
		public FileStream Open(FileMode mode, FileAccess access, FileShare share)
		{
			if (!(_file is FileInfo)) throw new InvalidOperationException();
			return File.Open(_file.FullName, mode, access, share);
		}
		
		public FileStream Open(FileMode mode, FileAccess access)
		{
			return this.Open(mode, access, FileShare.None);
		}
		
		public FileStream Open(FileMode mode)
		{
			return this.Open(mode, FileAccess.ReadWrite, FileShare.None);
		}
		
		public FileStream Open()
		{
			return this.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
		}
		
		public void Delete()
		{
			_file.Delete();
			this.Items.Clear();
		}
		
		public int Add(string streamName)
		{
			int index = this.IndexOf(streamName);
			if (-1 == index)
			{
				StreamInfo item = new StreamInfo(FileSystem.GetAlternateDataStream(_file, streamName, FileMode.OpenOrCreate));
				
				index = this.Items.Count;
				this.InsertItem(index, item);
			}
			
			return index;
		}
		
		public bool Remove(string streamName)
		{
			int index = this.IndexOf(streamName);
			if (-1 == index) return false;
			
			this.RemoveAt(index);
			return true;
		}
		
		protected override void ClearItems()
		{
			if (0 != this.Count)
			{
				foreach (StreamInfo item in this.Items)
				{
					item.Delete();
				}
			}
			
			base.ClearItems();
		}
		
		protected override void RemoveItem(int index)
		{
			this[index].Delete();
			base.RemoveItem(index);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 28 Jul 2010
Article Copyright 2002 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid