C#在文件中存储/加载guid列表

问题描述

是否可以在文件中存储和加载 guid 列表?

List<Guid> targetFaceIds = new List<Guid>();
        foreach (var imageFileName in targetimageFileNames)
        {
            List<DetectedFace> detectedFaces = await DetectFaceRecognize(client,_imagePath + imageFileName,recognitionModel);
            targetFaceIds.Add(detectedFaces[0].FaceId.Value);
        }

在我的程序中,我想将“targetFaceIds”存储在一个文件中是可能的,我如何才能将它从我的程序中的文件中加载回来。

在我的“targetFaceIds”中是这样的:

enter image description here

解决方法

您可以使用 guid.ToString 将其转换为字符串并使用 Guid.Parse 将其解析回 Guid

File.WriteAllLines(path,targetFaceIds.Select(g => g.ToString())); // write
targetFaceIds = File.ReadAllLines(path).Select(Guid.Parse).ToList(); // read