Working C # example: Writing and reading NTFS Alternative data stream under Win7 64-bit

I would like to be able to use an alternative data stream to store some synchronization information for the files referenced in the database application that I am creating.

However, every approach I found on the Internet has somehow failed. Having no programming experience with the Win32 API, I don't know why crashes occur. In some cases, method calls seem successful, only no alternative stream is created (i.e. I cannot read from the alternative file later, and also can not see AlternateStreamViewer). In other cases, writing succeeds, but reading fails because calling CreateFile () results in an invalid SafeFileHandle.

At this point, I just wanted to find some kind of working code. Or keep in mind that "I can't get from here." In this case, I will use another, less attractive option (for example, encode synchronization / link information in the file name itself and hope that no one ever changes the file name).


I understand the request for what approaches I tried. But there were many (I spent several hours on this), and it was very interesting for me to learn about the offers that I had not tried.

However, you are right that this would help, because, as it turned out, I used one of the approaches - Trinet, mentioned below - is wrong. I think the problem is that I have not yet created a “base” file for which I tried to save an alternate stream.

Here are the links to the codebases I tried:

+6
source share
2 answers

I 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 that I bookmarked isn’t there right now, so I’ll try to publish the code when backing it up if you still have problems.

Edit: Apparently, it's that simple:

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

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

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

+2
source

Source: https://habr.com/ru/post/915057/


All Articles