关闭读取的文件
This commit is contained in:
parent
6ec0677555
commit
5daa5b987a
@ -44,23 +44,26 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Encoding hostsEncoding;
|
||||||
var hasConflicting = false;
|
var hasConflicting = false;
|
||||||
var hostsBuilder = new StringBuilder();
|
var hostsBuilder = new StringBuilder();
|
||||||
using var fileStream = new FileStream(hostsPath, FileMode.Open, FileAccess.Read);
|
using (var fileStream = new FileStream(hostsPath, FileMode.Open, FileAccess.Read))
|
||||||
using var streamReader = new StreamReader(fileStream);
|
|
||||||
|
|
||||||
while (streamReader.EndOfStream == false)
|
|
||||||
{
|
{
|
||||||
var line = await streamReader.ReadLineAsync();
|
using var streamReader = new StreamReader(fileStream);
|
||||||
if (this.IsConflictingLine(line))
|
while (streamReader.EndOfStream == false)
|
||||||
{
|
{
|
||||||
hasConflicting = true;
|
var line = await streamReader.ReadLineAsync();
|
||||||
hostsBuilder.AppendLine($"# {line}");
|
if (this.IsConflictingLine(line))
|
||||||
}
|
{
|
||||||
else
|
hasConflicting = true;
|
||||||
{
|
hostsBuilder.AppendLine($"# {line}");
|
||||||
hostsBuilder.AppendLine(line);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
hostsBuilder.AppendLine(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
hostsEncoding = streamReader.CurrentEncoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -68,8 +71,7 @@ namespace FastGithub.PacketIntercept.Dns
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
File.Move(hostsPath, Path.ChangeExtension(hostsPath, ".bak"), overwrite: true);
|
await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), hostsEncoding, cancellationToken);
|
||||||
await File.WriteAllTextAsync(hostsPath, hostsBuilder.ToString(), streamReader.CurrentEncoding, cancellationToken);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user