MARK havn’t incvoke ReadFile function
解决方案 »
- TCP/IP 异步 多线程 数据同步程序的优化,喜欢优化方案的朋友过来看看
- 在Visual Studio中怎么让图片框保存.psd格式的图片
- 求大文件上传技术的公司或个人
- 如何通过textBox输入函数形式,然后求函数值
- 怎样得到新磁盘插入的通知的消息?并确定新加入了哪个磁盘?
- 百货购物商城中关于商品的一个难题
- 关于 C# WebForm 里的一个问题,请帮忙解答!谢谢了先
- 正则表达式,取得table间内容。求教
- 安装vs.net2003出现这样的情况(急):错误1308。未找到源文件:....\2052\cpref.Hxs
- 急:c#中如何调用sqlserver的数据导入导出
- 怎么修改VS2005中使用向导建立的*.xsd文件如何(C#)修改其数据库连接字符串
- .NET 2.0 中FTP 出现远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。
{
const uint GENERIC_READ = 0x80000000;
const uint OPEN_EXISTING = 3;
System.IntPtr handle; [System.Runtime.InteropServices.DllImport("kernel32", SetLastError = true)]
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,
0,
OPEN_EXISTING,
0,
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 <FileName>");
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;
}
}
}