[C#] C#通过指针读取文件 →→→→→进入此内容的聊天室

来自 , 2020-05-12, 写在 C#, 查看 110 次.
URL http://www.code666.cn/view/3c8a4914
  1. // readfile.cs
  2. // 编译时使用:/unsafe
  3. // 参数:readfile.txt
  4.  
  5. // 使用该程序读并显示文本文件。
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9.  
  10. class FileReader
  11. {
  12.         const uint GENERIC_READ = 0x80000000;
  13.         const uint OPEN_EXISTING = 3;
  14.         IntPtr handle;
  15.  
  16.         [DllImport("kernel32", SetLastError=true)]
  17.         static extern unsafe IntPtr CreateFile(
  18.                 string FileName,                                // 文件名
  19.                 uint DesiredAccess,                             // 访问模式
  20.                 uint ShareMode,                                 // 共享模式
  21.                 uint SecurityAttributes,                // 安全属性
  22.                 uint CreationDisposition,               // 如何创建
  23.                 uint FlagsAndAttributes,                // 文件属性
  24.                 int hTemplateFile                               // 模板文件的句柄
  25.                 );
  26.  
  27.  
  28.  
  29.         [DllImport("kernel32", SetLastError=true)]
  30.         static extern unsafe bool ReadFile(
  31.                 IntPtr hFile,                                   // 文件句柄
  32.                 void* pBuffer,                          // 数据缓冲区
  33.                 int NumberOfBytesToRead,        // 要读取的字节数
  34.                 int* pNumberOfBytesRead,                // 已读取的字节数
  35.                 int Overlapped                          // 重叠缓冲区
  36.                 );
  37.  
  38.  
  39.         [DllImport("kernel32", SetLastError=true)]
  40.         static extern unsafe bool CloseHandle(
  41.                 IntPtr hObject   // 对象句柄
  42.                 );
  43.        
  44.         public bool Open(string FileName)
  45.         {
  46.                 // 打开现有文件进行读取
  47.                
  48.                 handle = CreateFile(
  49.                         FileName,
  50.                         GENERIC_READ,
  51.                         0,
  52.                         0,
  53.                         OPEN_EXISTING,
  54.                         0,
  55.                         0);
  56.        
  57.                 if (handle != IntPtr.Zero)
  58.                         return true;
  59.                 else
  60.                         return false;
  61.         }
  62.  
  63.         public unsafe int Read(byte[] buffer, int index, int count)
  64.         {
  65.                 int n = 0;
  66.                 fixed (byte* p = buffer)
  67.                 {
  68.                         if (!ReadFile(handle, p + index, count, &n, 0))
  69.                                 return 0;
  70.                 }
  71.                 return n;
  72.         }
  73.  
  74.         public bool Close()
  75.         {
  76.                 // 关闭文件句柄
  77.                 return CloseHandle(handle);
  78.         }
  79. }
  80.  
  81. class Test
  82. {
  83.         public static int Main(string[] args)
  84.         {
  85.                 if (args.Length != 1)
  86.                 {
  87.                         Console.WriteLine("Usage : ReadFile <FileName>");
  88.                         return 1;
  89.                 }
  90.                
  91.                 if (! System.IO.File.Exists(args[0]))
  92.                 {
  93.                         Console.WriteLine("File " + args[0] + " not found.");
  94.                         return 1;
  95.                 }
  96.  
  97.                 byte[] buffer = new byte[128];
  98.                 FileReader fr = new FileReader();
  99.                
  100.                 if (fr.Open(args[0]))
  101.                 {
  102.                        
  103.                         // 假定正在读取 ASCII 文件
  104.                         ASCIIEncoding Encoding = new ASCIIEncoding();
  105.                        
  106.                         int bytesRead;
  107.                         do
  108.                         {
  109.                                 bytesRead = fr.Read(buffer, 0, buffer.Length);
  110.                                 string content = Encoding.GetString(buffer,0,bytesRead);
  111.                                 Console.Write("{0}", content);
  112.                         }
  113.                         while ( bytesRead > 0);
  114.                        
  115.                         fr.Close();
  116.                         return 0;
  117.                 }
  118.                 else
  119.                 {
  120.                         Console.WriteLine("Failed to open requested file");
  121.                         return 1;
  122.                 }
  123.         }
  124. }
  125. //csharp/4928

回复 "C#通过指针读取文件"

这儿你可以回复上面这条便签

captcha