Click here to Skip to main content
13,258,926 members (48,651 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 21 Nov 2008

ADSdotNET: A DLL for Using Alternate Data Streams from .NET Languages

, 24 Apr 2011
Rate this:
Please Sign up or sign in to vote.
With this class, you can use ADS with C#, VB.NET, and other .NET languages without P/Invoke


ADSdotNET is a library which makes it possible for .NET-based programming languages like C# or Visual Basic .NET to access alternate data streams (ADS). Without this DLL, it would be necessary to use P/Invoke or C++/CLI to work with ADS and to benefit from .NET-based languages at the same time.

ADSdotNET was developed in C++/CLI by the way. The zip file contains a Visual Studio 2005 project. The other zip files ( and contain the compiled DLL only.


When I tried to access ADS from my C# code, I found out that the .NET framework currently does not support file streams. So I decided to write a class which I could use for my C# project. I decided to use C++/CLI for writing ADSdotNET because in my opinion using this language for accessing the Windows API is more "natural" than using P/Invoke.

Using the Code

After including the DLL as reference in your Visual Studio project (you can do that in the Solution Explorer), you can see a new entry inside the folder References named ADSdotNET.

From now on, you can use a new namespace called AdsDotNet. It contains the class AdsHandler. First of all, you have to create an object of this class. Every object of type AdsHandler points to one file or directory. The methods of these objects can be used for adding/deleting/modifying the alternate data streams of the corresponding file or directory (will be called “root object” in the future). To root objects, alternate data streams can be attached.

Now I will describe the methods of the class AdsHandler. Examples will help to understand the usage (I hope).

Overview of all AdsHandler methods:


Creates a new object of type AdsHandler which represents a file or a folder on which the ADS operations can be executed.

C++/CLI declaration

AdsHandler(String ^filename);


filename: Name of an existing file or folder (= name of the root object)

Example 1

AdsManager adsman = new AdsManager("C:\\texts\\textfile.txt"); 

Example 2

AdsManager adsman = new AdsManager("C:\\texts"); 


By passing an empty string - new AdsManager(""); - a CommandNotAllowedException will be thrown. If the root object does not exist, a FileNotFoundException will be thrown.


Adds an empty ADS to the root object.

C++/CLI declaration

void AddAds(String ^adsname);


adsname: The name of the ADS which should be created.




By passing an empty string - adsman.AddAds(""); – a CommandNotAllowedException will be thrown. If an ADS with the same name already exists, nothing happens.


Removes (deletes) an ADS from the root object.

C++/CLI declaration

void DeleteAds(String ^adsname);


adsname: The name of the ADS which should be deleted.




By passing an empty string - adsman.AddAds(""); - the root object and all its ADS will be deleted if the root object was a file. Otherwise a WinAPIException will be thrown.


Returns true if a certain ADS exists, false otherwise.

C++/CLI declaration

bool AdsExists(String ^adsname);


adsname: The existence of an ADS with this name will be checked.


if(adsman.AdsExists("stream334")) { ... } 


By passing an empty string - adsman.AdsExists(""); -, the method checks the existence of the root object.


Returns a list of names of all ADS attached to the root object.

C++/CLI declaration

System::Collections::Generic::List<System::String^> ^GetAdsList(); 


List<String> adslist = adsman.GetAdsList();

Opens an ADS for reading/writing. A FileStream object will be returned so that you can work with ADS in the same manner as you can work with normal files.

C++/CLI declaration

System::IO::FileStream ^OpenAds(String ^adsname);


adsname: Name of the ADS which should be opened.


AdsHandler adsman_folder = new AdsHandler("testfolder");
System.IO.FileStream fs = adsman_folder.OpenAds("asd");
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
sw.WriteLine("Hello ADS!");


If the stream does not exist, a StreamNotFoundException will be thrown. By passing an empty string, a FileStream which points to the root file will be returned. If the root object is not a file, a WinAPIException will be thrown. The FileStream will be closed inside the method CloseAds. So you do not have to call fs.Close().


Returns true if an ADS with the name specified is opened at the moment, false otherwise. The stream must have been opened with the method OpenAds. So AdsIsOpened only returns true between the calls OpenAds and CloseAds (even if the FileStream was closed manually with fs.Close() before calling the method CloseAds).

C++/CLI declaration

bool AdsIsOpened(String ^adsname);


adsname: A stream name.


if (adsman_folder.AdsIsOpened("stream")) { ... }

Closes an ADS which was opened by OpenAds before. The FileStream which was provided by OpenAds will be closed by calling this method.

C++/CLI declaration

void CloseAds(String ^adsname);


adsname: The name of an open ADS.


(see OpenAds)


Builds a name like X:\path\file.txt:streamname or X:\path:streamname out of the filename and a stream name.

C++/CLI declaration

System::String ^GetCompleteFilename(String ^adsname);


adsname: A name which should be used as stream name.


string filename = adsman.GetCompleteFilename("stream"); 


A stream with the name specified does not have to exist.

More Resources

At, you can download a PDF file which contains the German version of the text you can read above.

AdsManager is an application written in C# which uses ADSdotNET. You can download the binary from Currently, I do not provide the source code of AdsManager.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Geier
Austria Austria
No Biography provided

You may also be interested in...


Comments and Discussions

Generala little help needed ;) Pin
h4ppyf4ce11-Aug-10 8:13
memberh4ppyf4ce11-Aug-10 8:13 
GeneralRe: a little help needed ;) Pin
mimao12-Aug-10 12:36
membermimao12-Aug-10 12:36 
GeneralPlz simple sample code Pin
moris200069-Jun-09 20:25
membermoris200069-Jun-09 20:25 
AnswerRe: Plz simple sample code [modified] Pin
Michael Geier9-Jun-09 23:34
memberMichael Geier9-Jun-09 23:34 
Here is a simple example for using the library.
First an ADS with the name "test" is attached to an existing file called "file.txt".
Then some text is written into the ADS "test". We have to reopen the ADS for reading, otherwise we get an ArgumentException ("The stream was not readable"). Remember to close the streams in the reverse order you have opened them.

AdsDotNet.AdsHandler adsh = new AdsDotNet.AdsHandler("file.txt");
FileStream fs = adsh.OpenAds("test");
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("hello world!");
// You have to close and reopen the ADS
// if you want read and write at the same
// stream.
fs = adsh.OpenAds("test");
StreamReader sr = new StreamReader(fs);
string txt = sr.ReadToEnd();


modified on Sunday, June 14, 2009 5:52 AM

GeneralAlternatively... Pin
Richard Deeming18-Dec-08 10:40
memberRichard Deeming18-Dec-08 10:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 25 Apr 2011
Article Copyright 2008 by Michael Geier
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid