Click here to Skip to main content
15,886,049 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 502.4K   5.4K   132  
A pair of classes to encapsulate access to NTFS alternative data streams.
using System;
using System.Drawing;

/// <summary>
/// A console app to test the NTFS class.
/// </summary>
namespace NTFS.Test
{
	class TestNTFS
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			TestStream();
			Console.ReadLine();
		}

		static void TestStream() 
		{
			const string IMAGE_FILE = @"test.jpg";
			const string THUMB_STREAM = "thumb";

			//List all of the alternative streams for the file:
			NTFS.FileStreams FS = new NTFS.FileStreams(IMAGE_FILE);
			Console.WriteLine(FS.FileName);
			foreach(NTFS.StreamInfo s in FS) Console.WriteLine("\t" + s.Name);
			
			
			//Using an alternative stream to store a thumbnail image:
			System.Drawing.Image thm;
			System.IO.FileStream FileStream;
			
			int i = FS.IndexOf(THUMB_STREAM);
			if (i==-1) 
			{
				//Thumbnail stream not found - create and store the thumbnail:
				Console.WriteLine("Creating thumbnail:");
				System.Drawing.Image img = System.Drawing.Image.FromFile(IMAGE_FILE);
				int Width = 100; int Height = 100;
				
				//Maintain aspect ratio:
				int lW = (int)(img.Width * Height / img.Height);
				int lH = (int)(img.Height * Width / img.Width);
				if (lW>Width) Height = lH;
				else if (lH>Height) Width = lW;
				thm = img.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
				
				//Save the thumbnail to the stream
				FS.Add(THUMB_STREAM);
				FileStream = FS[THUMB_STREAM].Open(System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
				thm.Save(FileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
				FileStream.Close();
				Console.WriteLine("Created thumbnail: Size {0}x{1}", thm.Width, thm.Height);
				
			}
			else 
			{
				//Thumbnail stream exists - read the thumbnail back
				Console.WriteLine("Thumbnail already exists!");
				FileStream = FS[THUMB_STREAM].Open(System.IO.FileMode.Open, System.IO.FileAccess.Read);
				thm = System.Drawing.Image.FromStream(FileStream);
				FileStream.Close();
				Console.WriteLine("Read thumbnail: Size {0}x{1}", thm.Width, thm.Height);
				
				//Remove the thumbnail stream, for demo purposes only!
				FS.Remove(THUMB_STREAM);
			}
		}
	}
}

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