减少不必要的hosts文件写入

This commit is contained in:
老九 2021-09-20 13:14:00 +08:00
parent a1284a4013
commit bd56241d54

View File

@ -39,12 +39,14 @@ namespace FastGithub.Dns
return; return;
} }
var hasConflicting = false;
var hostsBuilder = new StringBuilder(); var hostsBuilder = new StringBuilder();
var lines = await File.ReadAllLinesAsync(hostsPath, cancellationToken); var lines = await File.ReadAllLinesAsync(hostsPath, cancellationToken);
foreach (var line in lines) foreach (var line in lines)
{ {
if (this.IsConflictingLine(line)) if (this.IsConflictingLine(line))
{ {
hasConflicting = true;
hostsBuilder.AppendLine($"# {line}"); hostsBuilder.AppendLine($"# {line}");
} }
else else
@ -53,8 +55,11 @@ namespace FastGithub.Dns
} }
} }
File.SetAttributes(hostsPath, FileAttributes.Normal); if (hasConflicting == true)
await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken); {
File.SetAttributes(hostsPath, FileAttributes.Normal);
await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), cancellationToken);
}
} }
/// <summary> /// <summary>