Using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;
Using system. IO;
Using system. Data. sqlclient;
Namespace winapp
{
Public class Sequence
{
[Dllimport ("kernel32.dll")]
Public static extern intptr createfilemapping (intptr hfile, intptr lpfilemappingattributes, uint flprotect, uint dwmaximumsizehigh,
Uint dwmaximumsizelow, string lpname );
[Dllimport ("kernel32.dll")]
Public static extern intptr mapviewoffile (intptr hfilemappingobject, uint
Dwdesiredaccess, uint dwfileoffsethigh, uint dwfileoffsetlow, intptr dwnumberofbytestomap );
[Dllimport ("kernel32.dll")]
Public static extern bool unmapviewoffile (intptr lpbaseaddress );
[Dllimport ("kernel32.dll")]
Public static extern bool closehandle (intptr hobject );
[Dllimport ("kernel32.dll")]
Public static extern intptr createfile (string lpfilename, int dwdesiredaccess, fileshare dw1_mode, intptr securityattrs,
Filemode dwcreationdisposition, int dwflagsandattributes, intptr htemplatefile );
[Dllimport ("kernel32.dll")]
Public static extern uint getfilesize (intptr hfile, intptr lpfilesizehigh );
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;
Public const int page_noaccess = 1;
Public const int page_readonly = 2;
Public const int page_readwrite = 4;
Public const int file_map_copy = 1;
Public const int file_map_write = 2;
Public const int file_map_read = 4;
Public static string mappingfileread ()
{
String result = "";
Try
{
Intptr vfilehandle = createfile (@ "C:/test.txt", 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)
{
Intptr vmappinghandle = createfilemapping (vfilehandle, intptr. Zero, page_readwrite, 0, 0 ,"~ Mappingtemp ");
If (vmappinghandle! = Intptr. Zero)
{
Intptr vhead = mapviewoffile (vmappinghandle, file_map_copy | file_map_read | file_map_write, 0, 0, intptr. Zero );
If (vhead! = Intptr. Zero)
{
Uint vsize = getfilesize (vfilehandle, intptr. Zero );
Byte [] res = new byte [vsize];
Marshal. Copy (vhead, res, 0, (INT) vsize );
Result = asciiencoding. Default. getstring (RES );
String [] _ STR = result. Split ('| ');
Result = _ STR [0];
/*
//************************************** *
// _ Intptr = marshal. readintptr (vhead );
Byte [] contentbyte1 = new byte [vsize];
For (INT I = 0; I <vsize; I ++)
{
Byte vtemp = marshal. readbyte (intptr) (INT) vhead + I ));
Contentbyte1 [I] = vtemp;
}
Console. writeline (asciiencoding. Default. getstring (contentbyte1); // All values
//************************************** *
Byte [] contentbyte = new byte [vsize];
For (INT I = 0; I <vsize; I ++)
{
Byte vtemp = marshal. readbyte (intptr) (INT) vhead + I ));
Contentbyte [I] = vtemp;
If (vtemp = '|') // sets the end of the mark to conform
{
Break;
}
}
Console. writeline (asciiencoding. Default. getstring (contentbyte); // value to be retrieved
Result = asciiencoding. Default. getstring (contentbyte );
//************************************** *
*/
Unmapviewoffile (vhead );
}
Closehandle (vmappinghandle );
}
Closehandle (vfilehandle );
}
}
Catch
{}
Return result;
}
Public static intptr invalidhandlevalue = new intptr (-1 );
Public static void mappingfilewrite (string Val)
{
Try
{
Intptr vfilehandle = createfile (@ "C:/test.txt", 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)
{
Intptr vmappinghandle = createfilemapping (vfilehandle, intptr. Zero, page_readwrite, 0, (uint32) (Val. Length + 1 ),"~ Mappingtemp ");
If (vmappinghandle! = Intptr. Zero)
{
Intptr vhead = mapviewoffile (vmappinghandle, file_map_copy | file_map_read | file_map_write, 0, 0, intptr. Zero );
If (vhead! = Intptr. Zero)
{
Byte [] by = asciiencoding. Default. getbytes (Val + "|"); // sets the end of the mark to conform to |
Marshal. Copy (by, 0, vhead, by. Length );
Unmapviewoffile (vhead );
}
Closehandle (vmappinghandle );
}
Closehandle (vfilehandle );
}
}
Catch
{}
}
Public static void mappingwrite1 (string Val)
{
Try
{
Intptr vmappinghandle = createfilemapping (invalidhandlevalue, intptr. Zero, page_readwrite, 0, (uint32) (Val. Length + 1 ),"~ Mappingtemp ");
If (vmappinghandle! = Intptr. Zero)
{
Intptr vhead = mapviewoffile (vmappinghandle, file_map_copy | file_map_read | file_map_write, 0, 0, intptr. Zero );
If (vhead! = Intptr. Zero)
{
Byte [] by = asciiencoding. Default. getbytes (Val + "|"); // sets the end of the mark to conform to |
Marshal. Copy (by, 0, vhead, by. Length );
Unmapviewoffile (vhead );
}
// Closehandle (vmappinghandle );
}
}
Catch
{}
}
Public static string mappingread1 ()
{
String result = "";
Try
{
Intptr vmappinghandle = createfilemapping (invalidhandlevalue, intptr. Zero, page_readwrite, 0, 7 ,"~ Mappingtemp ");
If (vmappinghandle! = Intptr. Zero)
{
Intptr vhead = mapviewoffile (vmappinghandle, file_map_copy | file_map_read | file_map_write, 0, 0, intptr. Zero );
If (vhead! = Intptr. Zero)
{
Byte [] bytdata = new byte [7];
Marshal. Copy (vhead, bytdata, 0, 7 );
Result = asciiencoding. Default. getstring (bytdata );
Unmapviewoffile (vhead );
}
Closehandle (vmappinghandle );
}
}
Catch
{
}
Return result;
}
}
}
This article from the csdn blog, source: http://blog.csdn.net/gaozhigang/archive/2010/05/25/5622319.aspx