NTFS备用数据流 - .NET

时间:2009-03-03 03:11:00

标签: c# .net ntfs alternate-data-stream

如何从.NET创建/删除/读取/写入/ NTFS备用数据流?

如果没有本机.NET支持,我会使用哪个Win32 API?另外,我如何使用它们,因为我不认为这是记录在案的?

5 个答案:

答案 0 :(得分:32)

这是C#的版本

using System.Runtime.InteropServices;

class Program
{
    static void Main(string[] args)
    {
        var mainStream = NativeMethods.CreateFileW(
            "testfile",
            NativeConstants.GENERIC_WRITE,
            NativeConstants.FILE_SHARE_WRITE,
            IntPtr.Zero,
            NativeConstants.OPEN_ALWAYS,
            0,
            IntPtr.Zero);

        var stream = NativeMethods.CreateFileW(
            "testfile:stream",
            NativeConstants.GENERIC_WRITE,
            NativeConstants.FILE_SHARE_WRITE,
            IntPtr.Zero,
            NativeConstants.OPEN_ALWAYS,
            0,
            IntPtr.Zero);
    }
}

public partial class NativeMethods
{

    /// Return Type: HANDLE->void*
    ///lpFileName: LPCWSTR->WCHAR*
    ///dwDesiredAccess: DWORD->unsigned int
    ///dwShareMode: DWORD->unsigned int
    ///lpSecurityAttributes: LPSECURITY_ATTRIBUTES->_SECURITY_ATTRIBUTES*
    ///dwCreationDisposition: DWORD->unsigned int
    ///dwFlagsAndAttributes: DWORD->unsigned int
    ///hTemplateFile: HANDLE->void*
    [DllImportAttribute("kernel32.dll", EntryPoint = "CreateFileW")]
    public static extern System.IntPtr CreateFileW(
        [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpFileName, 
        uint dwDesiredAccess, 
        uint dwShareMode, 
        [InAttribute()] System.IntPtr lpSecurityAttributes, 
        uint dwCreationDisposition, 
        uint dwFlagsAndAttributes, 
        [InAttribute()] System.IntPtr hTemplateFile
    );

}


public partial class NativeConstants
{

    /// GENERIC_WRITE -> (0x40000000L)
    public const int GENERIC_WRITE = 1073741824;

    /// FILE_SHARE_DELETE -> 0x00000004
    public const int FILE_SHARE_DELETE = 4;

    /// FILE_SHARE_WRITE -> 0x00000002
    public const int FILE_SHARE_WRITE = 2;

    /// FILE_SHARE_READ -> 0x00000001
    public const int FILE_SHARE_READ = 1;

    /// OPEN_ALWAYS -> 4
    public const int OPEN_ALWAYS = 4;
}

答案 1 :(得分:14)

它们没有本机.NET支持。您必须使用P / Invoke来调用本机Win32方法。

要创建它们,请使用filename.txt:streamname之类的路径呼叫CreateFile。如果你使用返回SafeFileHandle的interop调用,你可以使用它来构造一个你可以读取的文件流。写信给。

要列出文件中存在的流,请使用FindFirstStreamWFindNextStreamW(仅存在于Server 2003及更高版本 - 而不是XP)。

我不相信你可以删除一个流,除非通过复制文件的其余部分并从其中一个流中删除。将长度设置为0也可以,但我还没有尝试过。

您还可以在目录上拥有备用数据流。您可以像访问文件一样访问它们 - C:\some\directory:streamname

Streams可以在它们上设置压缩,加密和稀疏性,与默认流无关。

答案 2 :(得分:14)

此nuget包CodeFluent Runtime Client具有(在其他实用程序中)NtfsAlternateStream Class,支持创建/读取/更新/删除/枚举操作。

答案 3 :(得分:9)

答案 4 :(得分:4)

不在.NET中:

http://support.microsoft.com/kb/105763

#include <windows.h>
   #include <stdio.h>

   void main( )
   {
      HANDLE hFile, hStream;
      DWORD dwRet;

      hFile = CreateFile( "testfile",
                       GENERIC_WRITE,
                    FILE_SHARE_WRITE,
                                NULL,
                         OPEN_ALWAYS,
                                   0,
                                NULL );
      if( hFile == INVALID_HANDLE_VALUE )
         printf( "Cannot open testfile\n" );
      else
          WriteFile( hFile, "This is testfile", 16, &dwRet, NULL );

      hStream = CreateFile( "testfile:stream",
                                GENERIC_WRITE,
                             FILE_SHARE_WRITE,
                                         NULL,
                                  OPEN_ALWAYS,
                                            0,
                                         NULL );
      if( hStream == INVALID_HANDLE_VALUE )
         printf( "Cannot open testfile:stream\n" );
      else
         WriteFile(hStream, "This is testfile:stream", 23, &dwRet, NULL);
   }