Click here to Skip to main content
13,701,212 members
Click here to Skip to main content
Articles » Languages » C# » General » Downloads

Stats

419.4K views
4.8K downloads
129 bookmarked
Posted 31 Jul 2002
Licenced CPOL

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.
binaries
Trinet.Core.IO.Ntfs.dll
Trinet.Core.IO.Ntfs.XML
doc
Trinet.Core.IO.Ntfs.chm
Trinet.Core.IO.Ntfs.FxCop
other
Compatibility wrapper
test.jpg
Unblock Files
Trinet.Core.IO.Ntfs
Properties
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...

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