减少不必要的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;
}
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);
}
}
/// <summary>