1 Star 0 Fork 0

wb/JetbrainsConfig

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Properties.cs 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
wb 提交于 25天前 . 可运行版本,未完善
namespace JetbrainsConfig;
using System;
using System.IO;
class Prop
{
string key = "";
string value = "";
bool line = false;
public string Key
{
get => key;
set => key = value ?? throw new ArgumentNullException(nameof(value));
}
public string Value
{
get => value;
set => this.value = value ?? throw new ArgumentNullException(nameof(value));
}
public bool Line
{
get => line;
set => line = value;
}
public override string ToString()
{
if (line)
{
return key;
}
return key + "=" + value;
}
}
public class Properties
{
string path;
List<Prop> props = new List<Prop>();
Dictionary<string, int> dict = new Dictionary<string, int>();
public Properties(string filename)
{
this.path = filename ?? throw new ArgumentNullException(nameof(filename));
string line;
int lineLen = -1;
using (StreamReader sr = new StreamReader(filename))
{
while (sr.Peek() >= 0)
{
var prop = new Prop();
line = sr.ReadLine();
lineLen++;
if (string.IsNullOrEmpty(line) || line.StartsWith('#'))
{
prop.Key = line;
prop.Line = true;
props.Add(prop);
continue;
}
var strings = line.Split('=');
prop.Key = strings[0];
prop.Value = strings[1];
prop.Line = false;
props.Add(prop);
dict.Add(strings[0], lineLen);
}
}
}
public void Add(string key, string value)
{
if (dict.ContainsKey(key))
{
Prop p = props[dict[key]];
p.Value = value;
}
else
{
props.Add(new Prop() { Key = key, Value = value});
}
}
public void Save()
{
if (File.Exists(path))
{
File.Delete(path);
}
FileStream fileStream = File.Create(path);
StreamWriter sw = new StreamWriter(fileStream);
foreach (Prop p in props)
{
sw.WriteLine(p.ToString());
}
sw.Close();
fileStream.Close();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mbqw/jetbrains-config.git
git@gitee.com:mbqw/jetbrains-config.git
mbqw
jetbrains-config
JetbrainsConfig
master

搜索帮助