C # Memory Sharing and memory ing files

Source: Internet
Author: User

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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.