未修改代码:
Stopwatch stop = new Stopwatch();stop.Start();
string str = "";
foreach (var item in dicImage)
{
str += $"{item.Name}?";
foreach (var ty in item.Types)
{
str += $"{ty.Name},{ty.Rectangle.X},{ty.Rectangle.Y},{ty.Rectangle.Width},{ty.Rectangle.Height}!";
}
str = str.Substring(0, str.Length - 1);
str += ";";
}
if (str.Length > 0)
{
str = str.Substring(0, str.Length - 1);
}
FileStream fs = new FileStream("DicSave.config", FileMode.Create, FileAccess.Write, FileShare.None);
StreamWriter swr = new StreamWriter(fs);//这里写上你要保存的路径
swr.WriteLine(str);//按行写
swr.Close(); fs.Close();
stop.Stop();MessageBox.Show(stop.ElapsedMilliseconds.ToString());
//耗时742
修改后代码
Stopwatch stop = new Stopwatch();stop.Start();
StringBuilder str = new StringBuilder();
foreach (var item in dicImage)
{
str.Append($"{item.Name}?");
foreach (var ty in item.Types)
{
str.Append($"{ty.Name},{ty.Rectangle.X},{ty.Rectangle.Y},{ty.Rectangle.Width},{ty.Rectangle.Height}!");
}
str = str.Remove(str.Length - 1, 1);
str.Append(";");
}
if (str.Length > 0)
{
str = str.Remove(str.Length - 1, 1);
}
FileStream fs = new FileStream("DicSave.config", FileMode.Create, FileAccess.Write, FileShare.None);
StreamWriter swr = new StreamWriter(fs);//这里写上你要保存的路径
swr.WriteLine(str);//按行写
swr.Close(); fs.Close();
stop.Stop();MessageBox.Show(stop.ElapsedMilliseconds.ToString());
//耗时6