-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHosts.cs
More file actions
87 lines (75 loc) · 2.5 KB
/
Hosts.cs
File metadata and controls
87 lines (75 loc) · 2.5 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// ripple-server-switcher
// © osu!ripple
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
namespace BanYouClient
{
abstract class BaseHostsEntry
{
public abstract override string ToString();
}
class HostsEntry : BaseHostsEntry
{
public string ip;
public string domain;
public override string ToString() => $"{ip}\t{domain}";
public HostsEntry() { }
public HostsEntry(string ip, string domain)
{
this.ip = ip;
this.domain = domain;
}
public override bool Equals(object obj)
{
var other = obj as HostsEntry;
if (other == null)
return false;
return ip.Equals(other.ip) && domain.Equals(other.domain);
}
public override int GetHashCode() => ToString().GetHashCode();
}
class HostsFile
{
private string hostsFilePath;
public HostsFile(string path = null)
{
hostsFilePath = path ?? Environment.GetEnvironmentVariable("windir") + "\\system32\\drivers\\etc\\hosts";
if (File.Exists(hostsFilePath))
{
FileInfo fileInfo = new FileInfo(hostsFilePath);
if (fileInfo.IsReadOnly)
fileInfo.IsReadOnly = false;
}
}
public void Write(HostsEntry[] entrys)
{
FileStream fs = new FileStream(hostsFilePath, FileMode.Append, FileAccess.Write, FileShare.None);
using (StreamWriter writer = new StreamWriter(fs))
{
foreach (BaseHostsEntry entry in entrys)
{
writer.WriteLine(entry.ToString());
}
}
}
public void Remove()
{
FileStream fs = new FileStream(hostsFilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
ArrayList lines = new ArrayList();
using (StreamReader reader = new StreamReader(fs))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (!line.EndsWith(".ppy.sh"))
{
lines.Add(line);
}
}
}
File.WriteAllText(hostsFilePath, string.Join("\n", lines.ToArray()) + "\n");
}
}
}