Click here to Skip to main content
13,146,739 members (93,012 online)
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

387.7K views
4.7K downloads
129 bookmarked
Posted 31 Jul 2002

Accessing alternative data-streams of files on an NTFS volume

, 15 Aug 2016
A pair of classes to encapsulate access to NTFS alternative data streams.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Trinet.Core.IO.Ntfs;

static class Program
{
	static void Main(string[] args)
	{
		try
		{
			bool recurse = false;
			bool whatIf = false;
			List<string> directories = new List<string>();
			List<string> files = new List<string>();
			char[] invalidChars = Path.GetInvalidPathChars();
			
			foreach (string value in args)
			{
				if (string.Equals(value, "/recurse", StringComparison.OrdinalIgnoreCase))
				{
					recurse = true;
				}
				else if (string.Equals(value, "/whatif", StringComparison.OrdinalIgnoreCase))
				{
					whatIf = true;
				}
				else if (-1 == value.IndexOfAny(invalidChars))
				{
					if (File.Exists(value))
					{
						files.Add(value);
					}
					else if (Directory.Exists(value))
					{
						directories.Add(value);
					}
					else
					{
						Console.WriteLine("File not found: " + value);
					}
				}
				else
				{
					Console.WriteLine("Unknown argument: " + value);
				}
			}
			
			if (0 != directories.Count)
			{
				SearchOption option = recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
				files.AddRange(directories.SelectMany(d => Directory.GetFiles(d, "*", option)));
			}
			
			if (0 != files.Count)
			{
				var process = whatIf ? new Func<string, bool>(ProcessFile_WhatIf) : new Func<string, bool>(ProcessFile_Real);
				int count = files.Count(process);
				Console.WriteLine("Processed {0:D} files.", count);
			}
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex);
		}
	}
	
	static bool ProcessFile_Real(string path)
	{
		bool result = FileSystem.AlternateDataStreamExists(path, ZoneName);
		if (result)
		{
			// Clear the read-only attribute, if set:
			FileAttributes attributes = File.GetAttributes(path);
			if (FileAttributes.ReadOnly == (FileAttributes.ReadOnly & attributes))
			{
				attributes &= ~FileAttributes.ReadOnly;
				File.SetAttributes(path, attributes);
			}
			
			result = FileSystem.DeleteAlternateDataStream(path, ZoneName);
			if (result) Console.WriteLine("Process {0}", path);
		}
		
		return result;
	}
	
	static bool ProcessFile_WhatIf(string path)
	{
		bool result = FileSystem.AlternateDataStreamExists(path, ZoneName);
		if (result) Console.WriteLine("Process {0}", path);
		return result;
	}
	
	const string ZoneName = "Zone.Identifier";
}

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

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 15 Aug 2016
Article Copyright 2002 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid