Alternate Data Streams

From REALbasicWiki

Jump to: navigation, search
Overall article skill Skill ranges from beginner (green) to expert (red)

On Windows platforms which use the NTFS filesystem (Windows NT, 2000, XP, Vista, and 7), each file consists of one or more data streams. Most files contain only a stream for the actual data in the file plus an additional metadata stream used to populate the Summary tab of the file properties dialog.

A file may contain any number of data streams. All data streams which are not the main stream are called Alternate Data Streams (or ADS). An ADS may contain any data that a normal file can contain, independent of the datatype of the main stream.

ADS are similar in most respects to the Resource Fork concept used in Apple operating system. However they remain largely unused even by Microsoft-created software.

[edit] Creating and Accessing Alternate Data Streams

Accessing or creating an ADS is done in the same way that any other filesystem object is, by using the FolderItem and input/output streams while referring to the ADS' path. ADS paths are identical to the path of their parent file with the name of the ADS appended after a colon.

For example, let's assume that there is a file called C:\foo\bar.txt and this file has an ADS called frell. The data inside frell can be accessed like this:

dim f as FolderItem = GetFolderItem("C:\foo\bar.txt:frell")
dim tis as TextInputStream
dim info as String
tis = tis.open(f)
info = tis.ReadAll
tis.close

The above code accessed the ADS and read the data therein into a string.

Creating an ADS is just as simple:

dim f as FolderItem = GetFolderItem("C:\foo\bar.txt:frell")
dim tos as TextOutputStream
tos = tos.create(f)
tos.write("This Is An Alternate Data Stream!")
tos.close

The above code created the ADS called frell for the file C:\foo\bar.txt and put some text into it.

[edit] See also

Personal tools
related