gpt4 book ai didi

C# - 使用单个键从文本文件中获取多个值

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:25 25 4
gpt4 key购买 nike

我在一个文本文件中存储了共享一个键的多个值。文本文件如下所示:

Brightness 36 , Manual
BacklightCompensation 3 , Manual
ColorEnable 0 , None
Contrast 16 , Manual
Gain 5 , Manual
Gamma 122 , Manual
Hue 0 , Manual
Saturation 100 , Manual
Sharpness 2 , Manual
WhiteBalance 5450 , Auto

现在我想存储每个键(例如亮度)的 int 值和字符串值。

C# 的新手,找不到有用的东西。

谢谢

最佳答案

我建议使用自定义类型来存储这些设置,如下所示:

public enum DisplaySettingType
{
Manual, Auto, None
}

public class DisplaySetting
{
public string Name { get; set; }
public decimal Value { get; set; }
public DisplaySettingType Type { get; set; }
}

然后您可以使用以下使用 string.Split 的 LINQ 查询来获取所有设置:

decimal value = 0;
DisplaySettingType type = DisplaySettingType.None;

IEnumerable<DisplaySetting> settings = File.ReadLines(path)
.Select(l => l.Trim().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries))
.Where(arr => arr.Length >= 3 && decimal.TryParse(arr[1], out value) && Enum.TryParse(arr[2], out type))
.Select(arr => new DisplaySetting { Name = arr[0], Value = value, Type = type });

关于C# - 使用单个键从文本文件中获取多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com