Akses Aliran Data Ganti NTFS dalam .NET
Memanipulasi Aliran Data Ganti NTFS (ADS) adalah penting untuk pelbagai senario, termasuk keselamatan dan data bersembunyi. .NET menyediakan fungsi untuk membaca dan mengubah suai strim ini.
Membaca ADS
Untuk membaca strim data yang dilampirkan pada fail, gunakan fungsi CreateFileW:
using System.Runtime.InteropServices; 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 ); }
Panggil CreateFileW dengan nama fail diikuti dengan nama strim, dipisahkan dengan titik bertindih (:). Contohnya:
var stream = NativeMethods.CreateFileW("testfile:stream", ...);
Mengubah suai ADS
Untuk menulis atau mengubah suai strim, hanya gunakan pemegang fail yang dikembalikan untuk melaksanakan operasi I/O. Sebagai contoh, untuk menulis ke strim:
using System.Runtime.InteropServices; class Program { static void Main(string[] args) { var stream = NativeMethods.CreateFileW("testfile:stream", ...); NativeMethods.WriteFile(stream, ...); } } public partial class NativeMethods { /// Return Type: BOOL->int ///hFile: HANDLE->void* ///lpBuffer: LPCVOID->void* ///nNumberOfBytesToWrite: DWORD->unsigned int ///lpNumberOfBytesWritten: LPDWORD->DWORD* ///lpOverlapped: LPOVERLAPPED->OVERLAPPED* [DllImportAttribute("kernel32.dll", EntryPoint = "WriteFile")] [return: MarshalAsAttribute(UnmanagedType.Bool)] public static extern bool WriteFile( [InAttribute()] System.IntPtr hFile, [InAttribute()] System.IntPtr lpBuffer, uint nNumberOfBytesToWrite, [OutAttribute()] [MarshalAsAttribute(UnmanagedType.U4)] out uint lpNumberOfBytesWritten, [InAttribute()] System.IntPtr lpOverlapped ); }
Begitu juga, anda boleh memadamkan strim menggunakan:
using System.Runtime.InteropServices; class Program { static void Main(string[] args) { var stream = NativeMethods.CreateFileW("testfile:stream", ...); NativeMethods.DeleteFile(stream); } } public partial class NativeMethods { /// Return Type: BOOL->int ///lpFileName: LPCWSTR->WCHAR* [DllImportAttribute("kernel32.dll", EntryPoint = "DeleteFileW")] [return: MarshalAsAttribute(UnmanagedType.Bool)] public static extern bool DeleteFileW([InAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpFileName); }
Atas ialah kandungan terperinci Bagaimanakah Saya Boleh Membaca, Menulis dan Padam Aliran Data Ganti NTFS dalam .NET?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!