You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.MemoryMappedFiles;
|
|
using System.Text;
|
|
using System.Windows;
|
|
|
|
namespace Utils
|
|
{
|
|
public class MemoryHelper
|
|
{
|
|
public static string GetMemoryToken()
|
|
{
|
|
string memoryName = string.Empty;
|
|
string[] args = Environment.GetCommandLineArgs();
|
|
foreach (var arg in args)
|
|
{
|
|
if (arg.StartsWith("uid:"))
|
|
{
|
|
memoryName = arg;
|
|
}
|
|
}
|
|
|
|
var result = MemoryHelper.ReadMMF(memoryName);
|
|
|
|
if (result.isOk)
|
|
{
|
|
return result.content;
|
|
}
|
|
else
|
|
{
|
|
System.Environment.Exit(0);
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
/// <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))
|
|
{
|
|
StreamReader reader = new StreamReader(mmfStream);
|
|
string jwt = reader.ReadToEnd().Replace("\0","").TrimEnd();
|
|
return (true, jwt);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message);
|
|
return (false, null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|