Click here to Skip to main content
Full site     10M members (35.7K online)    

Accessing alternative data-streams of files on an NTFS volume

Introduction

Since NT 3.1, the NTFS file system has supported multiple data-streams for files. There has never been built-in support for viewing or manipulating these additional streams, but the Windows API functions include support for them with a special file syntax: Filename.ext:StreamName. Even Win9x machines can access the alternative data streams of files on any NTFS volume they have access to, e.g., through a mapped drive. Because the Scripting.FileSystemObject and many other libraries call the CreateFile API behind the scenes, even scripts have been able to access alternative streams quite easily (although enumerating the existing streams has always been tricky).

In .NET, however, it seems someone decided to add some checking to the format of filenames. If you attempt to open a FileStream on an alternative stream, you will get a "Path Format not supported" exception. I have been unable to find any class in the CLR that provides support for alternative data streams, so I decided to roll my own.

Update

I originally wrote this code six years ago, targeting v1 of the .NET Framework. Looking at the code now, it seems quite messy, and has several bugs and problems which were mentioned in the comments. I have since completely re-written the code for .NET v3.5, and (hopefully) fixed the bugs.

The new code is not compatible with the original version. However, I have included a sample compatibility wrapper which maps the old API to the new API. You can find these files under the "other/Compatibility wrapper" folder in the download.

Bugs / Issues Fixed

Using the Classes

The AlternateDataStreamInfo class represents the details of an individual stream, and provides methods to create, open, or delete the stream.

The static FileSystem class provides methods to retrieve the list of streams for a file, retrieve a specific stream from a file, determine whether a stream exists, and delete a specific stream.

All methods on the FileSystem class offer overloads which accept either a path or a FileSystemInfo object. The overloads which accept a FileSystemInfo object can also be invoked as extension methods.

Example:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Trinet.Core.IO.Ntfs;

...

FileInfo file = new FileInfo(path);

// List the additional streams for a file:
foreach (AlternateDataStreamInfo s in file.ListAlternateDataStreams())
{
    Console.WriteLine("{0} - {1} bytes", s.Name, s.Size);
}

// Read the "Zone.Identifier" stream, if it exists:
if (file.AlternateDataStreamExists("Zone.Identifier"))
{
    Console.WriteLine("Found zone identifier stream:");
    
    AlternateDataStreamInfo s = 
       file.GetAlternateDataStream("Zone.Identifier",
                                   FileMode.Open);
    using (TextReader reader = s.OpenText())
    {
        Console.WriteLine(reader.ReadToEnd());
    }
    
    // Delete the stream:
    s.Delete();
}
else
{
    Console.WriteLine("No zone identifier stream found.");
}

// Alternative method to delete the stream:
file.DeleteAlternateDataStream("Zone.Identifier");

Files Included

References

If you want more information on NTFS programming, or the C++ code I based this on, see Dino Esposito's MSDN article from March 2000: http://msdn.microsoft.com/en-us/library/ms810604.aspx [^].

History

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
QuestionNot showing all data
ledtech3
3 Jan '13 - 9:29 
GeneralWow :)
Vitaly Tomilov
25 Jun '12 - 10:47 
QuestionSerialization
John Vonesh
20 Apr '12 - 7:22 
AnswerRe: Serialization
Richard Deeming
20 Apr '12 - 7:37 
GeneralRe: Serialization
John Vonesh
20 Apr '12 - 9:02 
GeneralRe: Serialization [modified]
John Vonesh
20 Apr '12 - 9:57 
Questionerror
Prince iraq
16 Mar '12 - 0:11 
AnswerRe: error
Richard Deeming
16 Mar '12 - 3:04 
GeneralRe: error
Prince iraq
16 Mar '12 - 7:40 
Question2Tb Virtual Disk
nhchmg
27 Jun '11 - 20:16 
AnswerRe: 2Tb Virtual Disk
Richard Deeming
5 Jul '11 - 7:38 
QuestionHow to close the stream?
DOSSTONED
22 Feb '11 - 4:26 
AnswerRe: How to close the stream?
Richard Deeming
22 Feb '11 - 6:15 
GeneralRe: How to close the stream?
DOSSTONED
22 Feb '11 - 16:35 
GeneralConvenience methods
VenDiddy
6 Aug '10 - 8:22 
GeneralExample code still not running properly on mapped network drive [modified]
LastZolex
14 Jun '10 - 11:29 
GeneralRe: Example code still not running properly on mapped network drive
Richard Deeming
15 Jun '10 - 1:38 
GeneralRe: Example code still not running properly on mapped network drive [modified]
LastZolex
15 Jun '10 - 2:09 
GeneralRe: Example code still not running properly on mapped network drive [modified]
Ashutosh Bhawasinka
12 Mar '12 - 16:51 
GeneralThis article is a great help!
LastZolex
11 Jun '10 - 23:44 
GeneralNeed to Map Win32Exception to Appropriate .NET Exception
Andy Missico
30 Mar '10 - 13:07 
GeneralRe: Need to Map Win32Exception to Appropriate .NET Exception
Richard Deeming
27 Jul '10 - 7:23 
GeneralValidateStreamName Incorrect Uses GetInvalidFileNameChars
Andy Missico
30 Mar '10 - 12:36 
GeneralProblem accessing alternate data stream of a network shared drive
m.vinod85
9 Nov '09 - 10:40 
GeneralRe: Problem accessing alternate data stream of a network shared drive
Richard Deeming
10 Nov '09 - 2:02 
GeneralRe: Problem accessing alternate data stream of a network shared drive
m.vinod85
10 Nov '09 - 5:44 
GeneralRe: Problem accessing alternate data stream of a network shared drive
Richard Deeming
10 Nov '09 - 6:54 
Questionhow to compile these sourecs
hefeilixin
29 Sep '09 - 17:36 
AnswerRe: how to compile these sourecs
Richard Deeming
30 Sep '09 - 1:31 
Newsanother question
hefeilixin
9 Oct '09 - 21:44 
GeneralRe: another question
Richard Deeming
12 Oct '09 - 1:08 
GeneralRe: another question
John Simmons / outlaw programmer
11 Feb '11 - 1:51 
GeneralFileInfo question
Brendan Chong
16 Sep '09 - 7:33 
GeneralRe: FileInfo question
Richard Deeming
16 Sep '09 - 7:49 
GeneralEdit File summary
=>Joe<=
29 Jun '09 - 23:55 
GeneralRe: Edit File summary
Richard Deeming
30 Jun '09 - 1:42 
GeneralRe: Edit File summary
=>Joe<=
1 Jul '09 - 1:33 
GeneralPlz simple sample code [modified]
moris20006
9 Jun '09 - 18:55 
GeneralRe: Plz simple sample code
Richard Deeming
10 Jun '09 - 1:20 
GeneralRe: Plz simple sample code
moris20006
10 Jun '09 - 3:28 
GeneralBUG: Does not work in Vista SP1
remit.ukraine@gmail.com
28 Apr '09 - 20:48 
GeneralRe: BUG: Does not work in Vista SP1
Richard Deeming
29 Apr '09 - 1:21 
GeneralRe: BUG: Does not work in Vista SP1
ini183
29 Apr '09 - 2:19 
GeneralVery nice
GSerjo
25 Sep '08 - 12:51 
GeneralBug: BackupRead() documentation points to bug in this code.
scooter_jsm
16 Sep '08 - 7:40 
GeneralRe: Bug: BackupRead() documentation points to bug in this code.
Richard Deeming
19 Sep '08 - 8:33 
GeneralNot for 64-bit
John SMith 5634552745
11 Apr '07 - 17:56 
NewsRe: Not for 64-bit
unRheal
21 May '09 - 9:19 
GeneralUpdated NTFS.dll to .NET 2.0
Moomansun
23 Aug '06 - 14:01 
GeneralAttach ADS to folder
Dan Elebash
30 Jul '04 - 11:57 

Last Updated 28 Jul 2010 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013