18 changed files with 176 additions and 16 deletions
@ -0,0 +1,64 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO.MemoryMappedFiles; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Utils |
||||
|
{ |
||||
|
public class MemoryHelper |
||||
|
{ |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 写入映射文件
|
||||
|
/// </summary>
|
||||
|
/// <param name="mapname"></param>
|
||||
|
/// <param name="content"></param>
|
||||
|
public static bool WriteMMF(string mapname, string content) |
||||
|
{ |
||||
|
MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(mapname, 1000, MemoryMappedFileAccess.ReadWrite); |
||||
|
if (!string.IsNullOrEmpty(content)) |
||||
|
{ |
||||
|
using (var mmfStream = mmf.CreateViewStream()) |
||||
|
{ |
||||
|
using (var sw = new System.IO.StreamWriter(mmfStream)) |
||||
|
{ |
||||
|
sw.Write(content.Trim()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 读取映射文件
|
||||
|
/// </summary>
|
||||
|
/// <param name="mapname"></param>
|
||||
|
public static (bool isOk,string content) ReadMMF(string mapname) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname); |
||||
|
using (var mmfStream = mmf.CreateViewStream(0, 1000, MemoryMappedFileAccess.ReadWrite)) |
||||
|
{ |
||||
|
byte[] buffer = new byte[128]; |
||||
|
int nLength = 0; |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
do |
||||
|
{ |
||||
|
nLength = mmfStream.Read(buffer, 0, 128); |
||||
|
sb.AppendLine(System.Text.ASCIIEncoding.Default.GetString(buffer)); |
||||
|
|
||||
|
} while (nLength > 0); |
||||
|
|
||||
|
return (true, sb.ToString().Replace("\0", null).TrimEnd()); |
||||
|
} |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
return (false, null); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue