Share via


使用資料流程

本主題中的範例示範如何使用基本 NTFS 檔案系統資料流程。

此範例會建立名為 「TestFile」 的檔案,其大小為 16 個位元組。 不過,檔案也有名為 「Stream」 的額外 ::$DATA 資料流程類型,這會新增作業系統未報告的額外 23 個位元組。 因此,當您檢視檔案的檔案大小屬性時,只會看到檔案的預設 ::$DATA 資料流程大小。

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

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

  hFile = CreateFile( TEXT("TestFile"), // Filename
                      GENERIC_WRITE,    // Desired access
                      FILE_SHARE_WRITE, // Share flags
                      NULL,             // Security Attributes
                      OPEN_ALWAYS,      // Creation Disposition
                      0,                // Flags and Attributes
                      NULL );           // OVERLAPPED pointer
  if( hFile == INVALID_HANDLE_VALUE )
   {
    printf( "Cannot open TestFile\n" );
    return;
   }
  else
   {
    WriteFile( hFile,              // Handle
               "This is TestFile", // Data to be written
               16,                 // Size of data, in bytes
               &dwRet,             // Number of bytes written
               NULL );             // OVERLAPPED pointer
    CloseHandle( hFile );
    hFile = INVALID_HANDLE_VALUE;
   }

  hStream = CreateFile( TEXT("TestFile:Stream"), // Filename
                        GENERIC_WRITE,           // Desired access
                        FILE_SHARE_WRITE,        // Share flags
                        NULL,                    // Security Attributes
                        OPEN_ALWAYS,             // Creation Disposition
                        0,                       // Flags and Attributes
                        NULL );                  // OVERLAPPED pointer
  if( hStream == INVALID_HANDLE_VALUE )
    printf( "Cannot open TestFile:Stream\n" );
  else
   {
    WriteFile( hStream,                   // Handle
               "This is TestFile:Stream", // Data to be written
               23,                        // Size of data
               &dwRet,                    // Number of bytes written
               NULL);                     // OVERLAPPED pointer
    CloseHandle( hStream );
    hStream = INVALID_HANDLE_VALUE;
   }
}

如果您在命令提示字元 中輸入 TestFile ,則會顯示下列輸出:

This is TestFile

不過,如果您輸入 類型 TestFile:Stream字組,它會產生下列錯誤:

「檔案名、目錄名稱或磁片區標籤語法不正確。」

若要檢視 TestFile:stream 的內容,請使用下列其中一個命令:

其他 < TestFile:Stream

其他 < TestFile:Stream:$DATA

顯示的文字如下所示:

This is TestFile:Stream

檔案資料流程