Giter Club home page Giter Club logo

ntfs-streams's Introduction

NTFS Streams

A library for working with alternate data streams on NTFS file systems from .NET applications.

Originally published on CodeProject: http://www.codeproject.com/KB/cs/ntfsstreams.aspx

Now available as a NuGet package: https://www.nuget.org/packages/Trinet.Core.IO.Ntfs/

Install-Package Trinet.Core.IO.Ntfs

Introduction

Since NT 3.1, the NTFS file system has supported multiple data-streams for files. There has never been built-in support for viewing or manipulating these additional streams, but the Windows API functions include support for them with a special file syntax: Filename.ext:StreamName. Even Win9x machines can access the alternative data streams of files on any NTFS volume they have access to, e.g., through a mapped drive. Because the Scripting.FileSystemObject and many other libraries call the CreateFile API behind the scenes, even scripts have been able to access alternative streams quite easily (although enumerating the existing streams has always been tricky).

In .NET, however, it seems someone decided to add some checking to the format of filenames. If you attempt to open a FileStream on an alternative stream, you will get a "Path Format not supported" exception. I have been unable to find any class in the CLR that provides support for alternative data streams, so I decided to roll my own.

Using the Classes

The AlternateDataStreamInfo class represents the details of an individual stream, and provides methods to create, open, or delete the stream.

The static FileSystem class provides methods to retrieve the list of streams for a file, retrieve a specific stream from a file, determine whether a stream exists, and delete a specific stream.

All methods on the FileSystem class offer overloads which accept either a path or a FileSystemInfo object. The overloads which accept a FileSystemInfo object can also be invoked as extension methods.

Example:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Trinet.Core.IO.Ntfs;

...

FileInfo file = new FileInfo(path);

// List the additional streams for a file:
foreach (AlternateDataStreamInfo s in file.ListAlternateDataStreams())
{
    Console.WriteLine("{0} - {1} bytes", s.Name, s.Size);
}

// Read the "Zone.Identifier" stream, if it exists:
if (file.AlternateDataStreamExists("Zone.Identifier"))
{
    Console.WriteLine("Found zone identifier stream:");
    
    AlternateDataStreamInfo s = file.GetAlternateDataStream("Zone.Identifier", FileMode.Open);
    using (TextReader reader = s.OpenText())
    {
        Console.WriteLine(reader.ReadToEnd());
    }
    
    // Delete the stream:
    s.Delete();
}
else
{
    Console.WriteLine("No zone identifier stream found.");
}

// Alternative method to delete the stream:
file.DeleteAlternateDataStream("Zone.Identifier");

Files Included

  • The Trinet.Core.IO.Ntfs folder contains the source code.
  • The doc folder contains the documentation and FxCop project.
  • The other folder contains a compatibility wrapper for the original version of this code, and a sample to recursively delete the "Zone.Identifier" stream from all files in a given path.

References

This code was inspired by Dino Esposito's MSDN article from March 2000: http://msdn.microsoft.com/en-us/library/ms810604.aspx

License

Copyright (c) 2002-2018 Richard Deeming All rights reserved.

This code is free software: you can redistribute it and/or modify it under the terms of either

  • the Code Project Open License (CPOL) version 1 or later; or
  • the GNU General Public License as published by the Free Software Foundation, version 3 or later; or
  • the BSD 2-Clause License;

This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ntfs-streams's People

Contributors

richardd2 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ntfs-streams's Issues

How to create an Alternative Stream ?

Your examples show how to list, and read from alternative streams, but not how to create them. Does this library support the functionality to create the streams as well ? And if so, then how? @RichardD2

Strange File Stream, mysterious effects, how to handle/repair?

Hi,
thanks for keeping this project alive, I use it for some time already for a small tool to simply store checksums as ADS in my files.
Working great most of the time but I have a very strange PDF file that I cannot handle, I cannot add a new stream, even when copied to FATand back, and it even corrupts the powershell console window if I show it's content.

I hope you or someone can help me to find some code that can hanlde such a file and repair it somehow.

File(zip):
https://multiupload.biz/cdxnourm4wok/WXPMode_HowToGuide_MultiUpload.biz.zip.html

Environment:
Windows 10 x64 (1607 Build 14393.576)

File/Stream-Infos

PS C:\tmp2> Get-Item -stream * -Path .\WXPMode_HowToGuide.pdf


PSPath        : Microsoft.PowerShell.Core\FileSystem::C:\tmp2\WXPMode_HowToGuide.pdf::$DAT
PSParentPath  : Microsoft.PowerShell.Core\FileSystem::C:\tmp2
PSChildName   : WXPMode_HowToGuide.pdf::$DATA
PSDrive       : C
PSProvider    : Microsoft.PowerShell.Core\FileSystem
PSIsContainer : False
FileName      : C:\tmp2\WXPMode_HowToGuide.pdf
**Stream        : :$DATA**
Length        : 3520196

Trying to add a stream (same with your lib)

PS C:\tmp2> Set-Content -Path .\WXPMode_HowToGuide.pdf -Stream Test

Cmdlet Set-Content an der Befehlspipelineposition 1
Geben Sie Werte für die folgenden Parameter an:
Value[0]: Test
Value[1]:
Set-Content : Der alternative Datenstrom "Test" der Datei "C:\tmp2\WXPMode_HowToGuide.pdf" konnte nicht geöffnet
werden.
In Zeile:1 Zeichen:1
+ Set-Content -Path .\WXPMode_HowToGuide.pdf -Stream Test
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-Content], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SetContentCommand

Powershell that causes corruption to the output:

Get-Content -Path .\WXPMode_HowToGuide.pdf -Stream ':$DATA'
or
(Get-Item -stream * -Path .\WXPMode_HowToGuide.pdf)[0] | Get-Content

├⎼▒�┌ ⎼ << /S�≥ 9°75 /R⎺⎺├ 128 0 R /I┰°⎺ 1 0 R /ID [ < 680 37°1 8>4613°5▒02 475 9°680 37°1 8> ] >>5▒02 475 ⎽├▒⎼├│⎼ ° 3516533 %%EOF PS C:\├└⎻2>

PS C:\├└⎻2> dir


    V
⎼≥
�
┰�⎽: C:\├└⎻2


M⎺
                L▒⎽├W⎼�├
T�└
         L
┰±├
 N▒└

----                -------------         ------ ----
-----       23.12.2016     19:49                S├┤°°
-▒----       20.11.2015     20:24           7005 E┤┌▒.├│├
-▒⎼---       03.11.2009     19:04        3520196 WXPM⎺
_H⎺┬T⎺G┤�
°
-▒----       23.12.2016     19:46        3428688 WXPM⎺
_H⎺┬T⎺G┤�
.≥�⎻

Read Only Attribute on NTFS Stream

Capture

Hi,

i'm trying to change the content of an NTFS Stream on a Folder (not a file).
It works like a charm, except for some streams which have the "Read Only" attribute set.
Is there any way to change the attributes using your library?

Thanks,
Peter

Signing the DLL

Hi,

Could you sign the DLL? I did want to use it in a project with strong names (signed) and couldn't use the nuget package because it isn't. I ended up with compiling the source myself but It would be handy if I does not need to do that.

Greetings,
Kees

Nuget Package

Please consider making this a nuget package. I'd like to use your library this way. 👍

System.IO.FileNotFoundException on server share paths over 255 charracters...

When using .net 4.6.2 (which fixes accessing long file names/paths)
I'm using the \\?\UNC\server\share\Verrrrrrrrrrrry long path syntax.

https://blogs.msdn.microsoft.com/jeremykuhne/2016/06/21/more-on-new-net-path-handling/
https://stackoverflow.com/questions/5188527/how-to-deal-with-files-with-a-name-longer-than-259-characters

I get:

System.IO.FileNotFoundException: The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at Trinet.Core.IO.Ntfs.SafeNativeMethods.ThrowIOError(Int32 errorCode, String path)
at Trinet.Core.IO.Ntfs.SafeNativeMethods.ThrowLastIOError(String path)
at Trinet.Core.IO.Ntfs.SafeNativeMethods.SafeGetFileAttributes(String name)
at Trinet.Core.IO.Ntfs.FileSystem.AlternateDataStreamExists(FileSystemInfo file, String streamName)

looks like public static string BuildStreamPath(string filePath, string streamName) isn't handling if it is a server share vs local drive.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.