Working C# Example: Writing & Reading NTFS Alternate Data Stream Under Win7 64 bit

空扰寡人 提交于 2019-12-04 21:17:43

问题


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:

  • St. Croix Skipper
  • DevDump
  • Trinet

回答1:


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();
}



回答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



来源:https://stackoverflow.com/questions/10477059/working-c-sharp-example-writing-reading-ntfs-alternate-data-stream-under-win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!