-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIni.cs
More file actions
51 lines (42 loc) · 1.26 KB
/
Ini.cs
File metadata and controls
51 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace IniSharp
{
class Ini
{
public static Ini Create(string path)
{
File.WriteAllText(path, "", Encoding.Unicode);
return new Ini(path);
}
public Ini(string path)
{
Path = path;
}
public void SetValue(string name, object value)
{
var lines = File.ReadAllLines(Path).ToList();
string targetLine = string.Format("{0}={1}", name, value);
try
{
lines[lines.FindIndex(line => line.StartsWith(name, StringComparison.OrdinalIgnoreCase))] = targetLine;
}
catch (IndexOutOfRangeException)
{
lines.Add(targetLine);
}
File.WriteAllLines(Path, lines);
}
public T GetValue<T>(string name)
{
var line = File.ReadAllLines(Path).First(l => l.StartsWith(name, StringComparison.OrdinalIgnoreCase));
string value = line.Split(new [] {'='}, 2)[1];
return (T)Convert.ChangeType(value, typeof(T));
}
public string Path { get; set; }
}
}