7

I'd like to be able to use an alternate data stream to store some synchronization information for files that are referenced in a database application I'm building.

However, every approach I've found on the web has failed in one way or another. Not being experienced at Win32 API programming I'm not sure why the failures are occurring. In some cases the method calls appear to succeed, only no alternate stream is created (i.e., I can't later read from the alternate file, nor can AlternateStreamViewer see it). In other cases the write succeeds, but the read fails because calling CreateFile() results in an invalid SafeFileHandle.

At this point I'd just like to find some working code. Or be advised that "ya can't get there from here". In which case I'll use another, less attractive option (e.g., encode the sync/reference information in the filename itself, and hope no one ever changes a file name).


I understand the request for what approaches I tried. But there were quite a few (I spent hours on this), and I was really interested in learning about offerings I hadn't tried.

However, you're right that it would've helped, as it turns out I was using one of the approaches -- the Trinet one referred to below -- incorrectly. I think the problem was that I hadn't yet created the "base" file for which I was trying to save an alternate stream.

Here are links to the code bases I tried:

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

7

I've had success on Windows 7 x64 using this library:

https://github.com/hubkey/Trinet.Core.IO.Ntfs

I can't find my old code, and the documentation page I have bookmarked is down at the moment, so I'll try to post some code when it's back up if you're still having problems.

Edit: Apparently it's as simple as:

using Trinet.Core.IO.Ntfs;

var fileInfo = new FileInfo(@"C:\path\to\file.dat");
if (AlternateDataStreamExists("MyStreamName"))
{
    var alternateStream = fileInfo.GetAlternateDataStream("MyStreamName").OpenRead();
}
else
{
    var alternateStream = fileInfo.GetAlternateDataStream("MyStreamName").OpenWrite();
}
TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • 1
    It's only simple when you know how to do it :) Thanks, EvilPenguin! I had tried the Trinet solution, but used the wrong approach because the library seems to have changed since the CodeProject example was written. Also, I was creating the alternate stream prior to creating the "base" file, which may be problematic. In any event, it now works! –  May 07 '12 at 15:09
2

You can also try my Platform.VirtualFileSystem library which supports listing, reading and writing NTFS alternate data streams in just a few lines of code.

https://github.com/platformdotnet/Platform.VirtualFileSystem/wiki https://github.com/platformdotnet/Platform.VirtualFileSystem/wiki/Alternate-Data-Streams

tumtumtum
  • 1,052
  • 9
  • 11
  • your library supports paths with length > 260 when using "\\?\" alternate syntax. Trinet's library just fails on that. – Teoni Valois Jul 06 '17 at 13:33