public class ReadBigTxt
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
""#region""
[DllImport("kernel32.dll")]
public static extern IntPtr CreateFileMapping(IntPtr hFile,
IntPtr lpFileMappingAttributes, uint flProtect,
uint dwMaximumSizeHigh,
uint dwMaximumSizeLow, string lpName);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll")]
public static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint
dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow,
IntPtr dwNumberOfBytesToMap);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll")]
public static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll")]
public static extern IntPtr CreateFile(string lpFileName,
int dwDesiredAccess, FileShare dwShareMode, IntPtr securityAttrs,
FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
![](/Images/OutliningIndicators/InBlock.gif)
[DllImport("kernel32.dll")]
public static extern uint GetFileSize(IntPtr hFile, IntPtr lpFileSizeHigh);
![](/Images/OutliningIndicators/InBlock.gif)
public const int GENERIC_READ = -2147483648; //0x80000000
public const int GENERIC_WRITE = 0x40000000;
public const int GENERIC_EXECUTE = 0x20000000;
public const int GENERIC_ALL = 0x10000000;
public const int FILE_ATTRIBUTE_NORMAL = 0x80;
public const int FILE_FLAG_SEQUENTIAL_SCAN = 0x8000000;
public const int INVALID_HANDLE_VALUE = -1;
![](/Images/OutliningIndicators/InBlock.gif)
public const int PAGE_NOACCESS = 1;
public const int PAGE_READONLY = 2;
public const int PAGE_READWRITE = 4;
![](/Images/OutliningIndicators/InBlock.gif)
public const int FILE_MAP_COPY = 1;
public const int FILE_MAP_WRITE = 2;
public const int FILE_MAP_READ = 4;
#endregion
private string m_LogPath = "";
public ReadBigTxt()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
public string GetFileTxt(string filename)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string str = "";
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr vFileHandle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.Open, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero);
if (INVALID_HANDLE_VALUE != (int)vFileHandle)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr vMappingHandle = CreateFileMapping(
vFileHandle, IntPtr.Zero, PAGE_READWRITE, 0, 0, "temp");
if (vMappingHandle != IntPtr.Zero)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IntPtr vHead = MapViewOfFile(vMappingHandle,
FILE_MAP_COPY | FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, IntPtr.Zero);
if (vHead != IntPtr.Zero)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
uint vSize = GetFileSize(vFileHandle, IntPtr.Zero);
byte[] nb = new byte[vSize];
Marshal.Copy(vHead, nb, 0, nb.Length);
str = System.Text.Encoding.GetEncoding("GB2312").GetString(nb);
nb = null;
UnmapViewOfFile(vHead);
}
CloseHandle(vMappingHandle);
}
CloseHandle(vFileHandle);
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
return str.Replace("\r", "").Replace("\n", "");
![](/Images/OutliningIndicators/InBlock.gif)
}
}
posted on
2008-03-28 11:46
小黑三
阅读(
977)
评论()
编辑
收藏
举报