通过调用CreateFile和ReadFile等API函数说明此问题,代码参考MSDN:
http://msdn2.microsoft.com/zh-cn/library/2d9wy99d(vs.80).aspx
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ApiTest
... {
classFileReader
...{
constuintGENERIC_READ=0x80000000;
constuintOPEN_EXISTING=3;
System.IntPtrhandle;
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeSystem.IntPtrCreateFile
(
stringFileName,//filename
uintDesiredAccess,//accessmode
uintShareMode,//sharemode
uintSecurityAttributes,//SecurityAttributes
uintCreationDisposition,//howtocreate
uintFlagsAndAttributes,//fileattributes
inthTemplateFile//handletotemplatefile
);
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeboolReadFile
(
System.IntPtrhFile,//handletofile
void*pBuffer,//databuffer
intNumberOfBytesToRead,//numberofbytestoread
int*pNumberOfBytesRead,//numberofbytesread
intOverlapped//overlappedbuffer
);
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeboolCloseHandle
(
System.IntPtrhObject//handletoobject
);
publicboolOpen(stringFileName)
...{
//opentheexistingfileforreading
handle=CreateFile
(
FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
0,
0
);
if(handle!=System.IntPtr.Zero)
...{
returntrue;
}
else
...{
returnfalse;
}
}
publicunsafeintRead(byte[]buffer,intindex,intcount)
...{
intn=0;
fixed(byte*p=buffer)
...{
if(!ReadFile(handle,p+index,count,&n,0))
...{
return0;
}
}
returnn;
}
publicboolClose()
...{
returnCloseHandle(handle);
}
}
classProgram
...{
staticintMain(string[]args)
...{
if(args.Length!=1)
...{
System.Console.WriteLine("Usage:ReadFile<FileName>");
return1;
}
if(!System.IO.File.Exists(args[0]))
...{
System.Console.WriteLine("File"+args[0]+"notfound.");
return1;
}
byte[]buffer=newbyte[128];
FileReaderfr=newFileReader();
if(fr.Open(args[0]))
...{
//AssumethatanASCIIfileisbeingread.
System.Text.ASCIIEncodingEncoding=newSystem.Text.ASCIIEncoding();
intbytesRead;
do
...{
bytesRead=fr.Read(buffer,0,buffer.Length);
stringcontent=Encoding.GetString(buffer,0,bytesRead);
System.Console.Write("{0}",content);
}
while(bytesRead>0);
fr.Close();
return0;
}
else
...{
System.Console.WriteLine("Failedtoopenrequestedfile");
return1;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ApiTest
... {
classFileReader
...{
constuintGENERIC_READ=0x80000000;
constuintOPEN_EXISTING=3;
System.IntPtrhandle;
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeSystem.IntPtrCreateFile
(
stringFileName,//filename
uintDesiredAccess,//accessmode
uintShareMode,//sharemode
uintSecurityAttributes,//SecurityAttributes
uintCreationDisposition,//howtocreate
uintFlagsAndAttributes,//fileattributes
inthTemplateFile//handletotemplatefile
);
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeboolReadFile
(
System.IntPtrhFile,//handletofile
void*pBuffer,//databuffer
intNumberOfBytesToRead,//numberofbytestoread
int*pNumberOfBytesRead,//numberofbytesread
intOverlapped//overlappedbuffer
);
[System.Runtime.InteropServices.DllImport("kernel32",SetLastError=true)]
staticexternunsafeboolCloseHandle
(
System.IntPtrhObject//handletoobject
);
publicboolOpen(stringFileName)
...{
//opentheexistingfileforreading
handle=CreateFile
(
FileName,
GENERIC_READ,
0,
0,
OPEN_EXISTING,
0,
0
);
if(handle!=System.IntPtr.Zero)
...{
returntrue;
}
else
...{
returnfalse;
}
}
publicunsafeintRead(byte[]buffer,intindex,intcount)
...{
intn=0;
fixed(byte*p=buffer)
...{
if(!ReadFile(handle,p+index,count,&n,0))
...{
return0;
}
}
returnn;
}
publicboolClose()
...{
returnCloseHandle(handle);
}
}
classProgram
...{
staticintMain(string[]args)
...{
if(args.Length!=1)
...{
System.Console.WriteLine("Usage:ReadFile<FileName>");
return1;
}
if(!System.IO.File.Exists(args[0]))
...{
System.Console.WriteLine("File"+args[0]+"notfound.");
return1;
}
byte[]buffer=newbyte[128];
FileReaderfr=newFileReader();
if(fr.Open(args[0]))
...{
//AssumethatanASCIIfileisbeingread.
System.Text.ASCIIEncodingEncoding=newSystem.Text.ASCIIEncoding();
intbytesRead;
do
...{
bytesRead=fr.Read(buffer,0,buffer.Length);
stringcontent=Encoding.GetString(buffer,0,bytesRead);
System.Console.Write("{0}",content);
}
while(bytesRead>0);
fr.Close();
return0;
}
else
...{
System.Console.WriteLine("Failedtoopenrequestedfile");
return1;
}
}
}
}
特别要注意的是,由于代码中用到了unsafe代码,因此,编译的时候需要在项目属性中勾选允许编译Unsafe的选项:
否则会报:
Unsafe code may only appear if compiling with /unsafe
的错误。