Click here to Skip to main content
15,891,951 members
Articles / Programming Languages / C#

Accessing alternative data-streams of files on an NTFS volume

Rate me:
Please Sign up or sign in to vote.
4.85/5 (53 votes)
15 Aug 2016CPOL4 min read 503.6K   5.4K   132  
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)


Written By
Software Developer CodeProject
United Kingdom United Kingdom
I started writing code when I was 8, with my trusty ZX Spectrum and a subscription to "Input" magazine. Spent many a happy hour in the school's computer labs with the BBC Micros and our two DOS PCs.

After a brief detour into the world of Maths, I found my way back into programming during my degree via free copies of Delphi and Visual C++ given away with computing magazines.

I went straight from my degree into my first programming job, at Trinet Ltd. Eleven years later, the company merged to become ArcomIT. Three years after that, our project manager left to set up Nevalee Business Solutions, and took me with him. Since then, we've taken on four more members of staff, and more work than you can shake a stick at. Smile | :)

Between writing custom code to integrate with Visma Business, developing web portals to streamline operations for a large multi-national customer, and maintaining RedAtlas, our general aviation airport management system, there's certainly never a dull day in the office!

Outside of work, I enjoy real ale and decent books, and when I get the chance I "tinkle the ivories" on my Technics organ.

Comments and Discussions