在我的项目中经常会需要保存少量数据在硬盘上,客户可能需要自行修改,所以总结出下面这一小段方法,放在Startup里面执行:
private void LoadData()
{
string portFilePathName = Application.streamingAssetsPath + "/" + portFileName;
if (!File.Exists(portFilePathName))
{
string[] strs = new string[2];
strs[0] = "COM4";
strs[1] = "115200";
File.WriteAllLines(portFilePathName, strs);
}
else
{
string[] allLines = File.ReadAllLines(portFilePathName);
portName = allLines[0];
baudRate = int.Parse(allLines[1]);
}
}