Search code examples
c#visual-studiontfsalternate-data-stream

How to use Trine Core io NTFS?


So I have tried using trinet core io ntfs dll to get to alternative data streams in C# (using VS 2015), but I have a problem: i couldn't find a way to write into a file. The code I tried:

var fileinfo = new FileInfo(filename);
var altstream = fileinfo.GetAlternateDataStream("key").OpenWrite();
altstream.WriteLine(Convert.ToString(radix));
altstream.WriteLine(Convert.ToString(push));
altstream.WriteLine(Convert.ToString(mult));
altstream.WriteLine(Convert.ToString(fill));

It just throws an error message before debug:

'FileStream' does not contain a definition for 'WriteLine' and no extension method 'WriteLine' accepting a first argument of type 'FileStream' could be found (are you missing a using directive or an assembly reference?)

How should I do it? I can't find this on the web but need the use of alternative data streams for my project.


Solution

  • altstream is a FileStream instance. FileStream only understands bytes, not strings. In order to convert strings (characters) to bytes, you have to pick a character encoding system, for instance Ascii, UTF8, UTF16, etc.

    Once you've picked an encoding, you can use the StreamWriter class to be able to directly write strings to files:

    FileStream altstream = fileinfo.GetAlternateDataStream("key").OpenWrite();
    
    // Note that I've picked UTF8 as an encoding. You need to figure out
    // for yourself, based on your requirements or desires, what encoding
    // to use. UTF8 is a popular encoding though.
    StreamWriter altstreamWriter = new StreamWriter( altstream, Encoding.UTF8 );
    
    altstreamWriter.WriteLine(Convert.ToString(radix));
    altstreamWriter.WriteLine(Convert.ToString(push));
    altstreamWriter.WriteLine(Convert.ToString(mult));
    altstreamWriter.WriteLine(Convert.ToString(fill));