使用.NET 讀取和修改NTFS 備用資料流
NTFS 備用資料流(ADS) 是與常規檔案關聯的隱藏資料流新技術檔案系統(NTFS)。這些流可用於儲存附加資訊,例如使用者評論、版本歷史或多媒體內容,而不影響主文件資料。
閱讀 ADS
至要讀取 ADS,可以使用 CreateFileW 函數,並將 dwDesiredAccess 參數設為 GENERIC_WRITE。這將打開流以進行讀取和寫入。然後,您可以使用 ReadFile 函數讀取流的內容。
修改 ADS
要修改 ADS,您可以使用 CreateFileW 函數,並將 dwDesiredAccess 參數設為GENERIC_WRITE。這將打開流以進行讀取和寫入。然後,您可以使用 WriteFile 函數將新內容寫入流。
以下是如何讀取和修改 ADS 的 C# 範例:
using System.Runtime.InteropServices; class Program { static void Main(string[] args) { // Open the main file stream var mainStream = NativeMethods.CreateFileW( "testfile", NativeConstants.GENERIC_WRITE, NativeConstants.FILE_SHARE_WRITE, IntPtr.Zero, NativeConstants.OPEN_ALWAYS, 0, IntPtr.Zero); // Open the ADS stream var stream = NativeMethods.CreateFileW( "testfile:stream", NativeConstants.GENERIC_WRITE, NativeConstants.FILE_SHARE_WRITE, IntPtr.Zero, NativeConstants.OPEN_ALWAYS, 0, IntPtr.Zero); // Write data to the ADS stream var data = "Hello world!"; NativeMethods.WriteFile(stream, data, data.Length, out var bytesWritten, IntPtr.Zero); // Close the ADS stream NativeMethods.CloseHandle(stream); // Close the main file stream NativeMethods.CloseHandle(mainStream); } } 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 ); /// Return Type: BOOL->int ///hFile: HANDLE->void* ///lpBuffer: LPVOID->void* ///nNumberOfBytesToWrite: DWORD->unsigned int ///lpNumberOfBytesWritten: LPDWORD->DWORD* ///lpOverlapped: LPOVERLAPPED->_OVERLAPPED* [DllImportAttribute("kernel32.dll", EntryPoint = "WriteFile")] public static extern int WriteFile( System.IntPtr hFile, [InAttribute()] System.IntPtr lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [InAttribute()] System.IntPtr lpOverlapped ); /// Return Type: BOOL->int ///hObject: HANDLE->void* [DllImportAttribute("kernel32.dll", EntryPoint = "CloseHandle")] public static extern int CloseHandle( [InAttribute()] System.IntPtr hObject ); } 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; }
以上是如何使用 .NET 讀取和修改 NTFS 備用資料流?的詳細內容。更多資訊請關注PHP中文網其他相關文章!