不是很清楚这个名字对不对。
有时候我们需要用unity获取cpu或者硬盘的序列号,但是SystemInfo并不能获取,我们只能通过C#来获取,这个时候就用到了流式数据读取。
具体体现为:通过winfrom程序将cpu或硬盘的序列号读取出来,然后unity通过数据流读取的方式获取信息。
以下为winform中的代码
private void Form1_Load(object sender, EventArgs e)
{
//GetCPUSerialNum();
StringBuilder log = new StringBuilder();
try
{
long s = 0;
long f = 0;
log.AppendLine(GetCPUSerialNum());
DriveInfo[] ds = DriveInfo.GetDrives();
foreach (DriveInfo d in ds)
{
s += d.TotalSize;
f += d.TotalFreeSpace;
}
//log.AppendLine("Harddisk Totalspace" + link + (s / 1048576).ToString());
//log.AppendLine("Harddisk Freespace" + link + (f / 1048576).ToString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
Console.Write(log.ToString());
}
private static string GetCPUSerialNum()
{
string cpuSerialNum = string.Empty;
try
{
ManagementClass manc = new ManagementClass("Win32_Processor");
ManagementObjectCollection manoc = manc.GetInstances();
foreach (ManagementObject mano in manoc)
{
cpuSerialNum = mano["ProcessorId"].ToString();
break;
}
manc.Dispose();
manoc.Dispose();
}
catch (Exception ex)
{
}
return cpuSerialNum;
}
下面是Unity流式读取的代码
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class GetCpuNember : MonoBehaviour
{
void Start()
{
StartCoroutine(Open());
}
private IEnumerator Open(string arguments = null)
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
//设置.net的程序路径
p.StartInfo.FileName = Application.streamingAssetsPath + "/GetHardDisk/QCSub.exe";
p.StartInfo.Arguments = arguments;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.StandardOutputEncoding = Encoding.Default;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
StreamReader s = p.StandardOutput;
yield return new WaitForSeconds(3f);
p.Kill();
p.WaitForExit();
Manager(s.ReadToEnd());
s.Close();
}
private void Manager(string content)
{
print(content);
//处理接收到的内容
}
}