c#语⾔read函数,如何:使⽤WindowsReadFile函数(C#编程
指南)
如何:使⽤ Windows ReadFile 函数(C# 编程指南)
12/17/2015
本⽂内容
下⾯的⽰例通过读取并显⽰⼀个⽂本⽂件来演⽰ Windows ReadFile 函数。 ReadFile 函数需要使⽤ unsafe 代码,因为它需要⼀个作为参数的指针。
传递到 Read 函数的字节数组是托管类型。 这意味着公共语⾔运⾏时 (CLR) 垃圾回收器可能会随意地对数组使⽤的内存进⾏重新定位。 为了防⽌出现这种情况,使⽤ fixed 来获取指向内存的指针并对它进⾏标记,以便垃圾回收器不会移动它。 在 fixed 块的末尾,内存将⾃动返回,以便能够通过垃圾回收移动。
此功能称为“声明式锁定”。 使⽤锁定,系统开销会⾮常⼩,除⾮在 fixed 块中发⽣垃圾回收(但此情况不太可能发⽣)。 但是,锁定会在全局垃圾回收运⾏过程中导致某些不利的副作⽤。 垃圾回收器压缩内存的能⼒在很⼤程度上受到锁定缓冲区的限制。 因此,应尽可能避免使⽤锁定。
⽰例
class FileReader
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
System.IntPtr handle;
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
static extern unsafe System.IntPtr CreateFile
(
string FileName, // file name
uint DesiredAccess, // access mode
uint ShareMode, // share mode
uint SecurityAttributes, // Security Attributes
uint CreationDisposition, // how to create
uint FlagsAndAttributes, // file attributes
int hTemplateFile // handle to template file
);
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)]
static extern unsafe bool ReadFile
(
System.IntPtr hFile, // handle to file
void* pBuffer, // data buffer
int NumberOfBytesToRead, // number of bytes to read
int* pNumberOfBytesRead, // number of bytes read
int Overlapped // overlapped buffer
);
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)] static extern unsafe bool CloseHandle
(
System.IntPtr hObject // handle to object
);
public bool Open(string FileName)
{
// open the existing file for reading
handle = CreateFile
(
FileName,
GENERIC_READ,
0,
writeline函数0,
OPEN_EXISTING,
0,
);
if (handle != System.IntPtr.Zero)
{
return true;
}
else
{
return false;
}
}
public unsafe int Read(byte[] buffer, int index, int count)
{
int n = 0;
fixed (byte* p = buffer)
{
if (!ReadFile(handle, p + index, count, &n, 0))
{
return 0;
}
}
return n;
}
public bool Close()
{
return CloseHandle(handle);
}
}
class Test
{
static int Main(string[] args)
{
if (args.Length != 1)
{
System.Console.WriteLine("Usage : ReadFile ");
return 1;
}
if (!System.IO.File.Exists(args[0]))
{
System.Console.WriteLine("File " + args[0] + " not found.");
return 1;
}
byte[] buffer = new byte[128];
FileReader fr = new FileReader();
if (fr.Open(args[0]))
{
// Assume that an ASCII file is being read.
System.Text.ASCIIEncoding Encoding = new System.Text.ASCIIEncoding();
int bytesRead;
do
{
bytesRead = fr.Read(buffer, 0, buffer.Length);
string content = Encoding.GetString(buffer, 0, bytesRead); System.Console.Write("{0}", content);
}
while (bytesRead > 0);
fr.Close();
return 0;
}
else
{
System.Console.WriteLine("Failed to open requested file"); return 1;
}
}
}
请参阅