I have been working on a C# project on Windows where the use of Alternate Data Streams seemed appropriate, but found out to my dismay that the .NET framework simply doesn't support them.
Alternate Data Streams are a feature of NTFS that allow a file to contain, in addition to the primary data, additional data that's maintained on the side, and I believe it was conceived to support "resource forks" on the Mac. These are not well-known features, though Windows does use them for storing metadata about some kinds of files, such as an image thumbnail or noting the fact that a file was downloaded from the internet. I was going to use Alternate Data Streams for file metadata.
Alternate data streams are accessed with a stream name after the filename, separated by a colon, so that a base file might be C:\Data\Foo.txt, but a named stream might be C:\Data\foo.txt:mystream. A file can have multiple named streams, and streams are generally invisible unless asked for specifically by name.
The .NET framework's pathname parser allows a colon only after a drive letter, so the use of one as a stream name separator yields an exception, as does the same filename when prefixed with the \\?\ notation used to disable path parsing in Win32.
This bit of code shows what happens:
/* test.cs - C# code demonstrating failure of alternate data streams */
using System;
using System.IO;
namespace Unixwiz {
public class Test {
public static void Main(string [] args)
{
string filename = @"C:\Data\foo.txt:mystream";
tryfile( filename );
tryfile( @"\\?\" + filename );
}
public static void tryfile(string name)
{
try
{
Console.WriteLine();
Console.WriteLine("Trying to open file {0}", name);
File.ReadAllText( name );
Console.WriteLine(" OK!");
}
catch ( Exception ex )
{
Console.WriteLine(" Exception: {0}", ex.GetType());
Console.WriteLine(" Message: {0}", ex.Message);
}
}
}
}
Running this shows the two kinds of exceptions generated:
C:> test.exe Trying to open file C:\Data\foo.txt:mystream Exception: System.NotSupportedException Message: The given path's format is not supported. Trying to open file \\?\C:\Data\foo.txt:mystream Exception: System.ArgumentException Message: Illegal characters in path.
I believe that one can get around this by the use of the Win32 CreateFile() function via PInvoke or the like, but decided not to go this route because I was likely to run into more surprises later when the cost of changing direction was much higher.
Useful resources:
- Accessing alternative data-streams of files on an NTFS volume — on CodeProject
- NTFS Alternate Data Streams - .NET — on StackOverflow





This Raymond Chen article http://blogs.msdn.com/b/oldnewthing/archive/2012/05/01/10299322.aspx explains that MS dropped alternate data streams for summary information support in Vista. Combined with the absence of a .NET API, I wonder if they are looking to phase them out generally.
Posted by: Will Watts | May 22, 2012 at 05:07 AM
Great and nice post thank you.
Posted by: Roger Vivier | April 10, 2013 at 05:35 PM