Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

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.
NtfsStreams.zip
bin
Trinet.Core.IO.Ntfs.dll
doc
Trinet.Core.IO.Ntfs.chm
Trinet.Core.IO.Ntfs.FxCop
other
Compatibility wrapper
test.jpg
Unblock Files
Trinet.Core.IO.Ntfs
Trinet.Core.IO.Ntfs.sln.cache
Properties
ntfsstreams_src.zip
test.jpg
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)

Share

About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
No Biography provided
Follow on   Twitter   Google+

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