From bd56241d54fedc84652c5539a76b6d8840ce1ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Mon, 20 Sep 2021 13:14:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=E4=B8=8D=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84hosts=E6=96=87=E4=BB=B6=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/HostsConflictSolver.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/FastGithub.Dns/HostsConflictSolver.cs b/FastGithub.Dns/HostsConflictSolver.cs index 58b07d6..362d96a 100644 --- a/FastGithub.Dns/HostsConflictSolver.cs +++ b/FastGithub.Dns/HostsConflictSolver.cs @@ -39,12 +39,14 @@ namespace FastGithub.Dns return; } + var hasConflicting = false; var hostsBuilder = new StringBuilder(); var lines = await File.ReadAllLinesAsync(hostsPath, cancellationToken); foreach (var line in lines) { if (this.IsConflictingLine(line)) { + hasConflicting = true; hostsBuilder.AppendLine($"# {line}"); } else @@ -53,8 +55,11 @@ namespace FastGithub.Dns } } - File.SetAttributes(hostsPath, FileAttributes.Normal); - await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken); + if (hasConflicting == true) + { + File.SetAttributes(hostsPath, FileAttributes.Normal); + await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken); + } } ///