C# 文件行没有被替换,即使我编码它这样做?
我试着浏览一个文件中的每一行,这是一个链接。然后我将所有的死链接(返回404或在页面上打印特定短语的链接)添加到一个字符串列表中。到目前为止一切正常,但我目前面临的唯一问题是,在接近尾声时,没有按照我的请求将它们从文件中删除 为什么会这样C# 文件行没有被替换,即使我编码它这样做?,c#,.net,C#,.net,我试着浏览一个文件中的每一行,这是一个链接。然后我将所有的死链接(返回404或在页面上打印特定短语的链接)添加到一个字符串列表中。到目前为止一切正常,但我目前面临的唯一问题是,在接近尾声时,没有按照我的请求将它们从文件中删除 为什么会这样 using System.Collections.Generic; namespace dead_link_finder { using System; using System.IO; using System.Net; u
using System.Collections.Generic;
namespace dead_link_finder
{
using System;
using System.IO;
using System.Net;
using System.Threading;
static class Program
{
private static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
var fileToScan = Console.ReadLine();
var reader = new StreamReader(fileToScan);
string line;
var deadLinks = new List<string>();
while ((line = reader.ReadLine()) != null)
{
Log("Scanning: " + line, ConsoleColor.White);
using (var webClient = new WebClient())
{
try
{
var content = webClient.DownloadString(line);
if (content.Contains("text-danger"))
{
deadLinks.Add(line);
}
}
catch (WebException wex)
{
if (((HttpWebResponse)wex.Response).StatusCode == HttpStatusCode.NotFound)
{
deadLinks.Add(line);
}
}
}
}
reader.Close();
Console.WriteLine();
Console.WriteLine("Found: " + deadLinks.Count + " dead links in the collection.");
Console.WriteLine();
Thread.Sleep(5000);
Console.WriteLine("Removing the dead links, please wait...");
foreach (var deadLink in deadLinks)
{
var str = File.ReadAllText(fileToScan);
File.WriteAllText(fileToScan, str.Replace(deadLink, ""));
}
Console.WriteLine();
Console.WriteLine("Finished...");
Console.ReadKey(true);
}
}
}
使用System.Collections.Generic;
命名空间死链接查找器
{
使用制度;
使用System.IO;
Net系统;
使用系统线程;
静态类程序
{
私有静态void Main(字符串[]args)
{
Console.ForegroundColor=ConsoleColor.White;
var fileToScan=Console.ReadLine();
var reader=新的StreamReader(fileToScan);
弦线;
var deadLinks=新列表();
而((line=reader.ReadLine())!=null)
{
日志(“扫描:+行,控制台颜色.白色”);
使用(var webClient=new webClient())
{
尝试
{
var content=webClient.DownloadString(行);
if(content.Contains(“文本危险”))
{
添加(行);
}
}
捕获(WebException wex)
{
if(((HttpWebResponse)wex.Response).StatusCode==HttpStatusCode.NotFound)
{
添加(行);
}
}
}
}
reader.Close();
Console.WriteLine();
Console.WriteLine(“找到:“+deadLinks.Count+”集合中的死链接”);
Console.WriteLine();
睡眠(5000);
WriteLine(“删除死链接,请稍候…”);
foreach(死链接中的var死链接)
{
var str=File.ReadAllText(fileToScan);
File.WriteAllText(fileToScan,str.Replace(死链接“”);
}
Console.WriteLine();
控制台。WriteLine(“完成…”);
Console.ReadKey(true);
}
}
}
您可以尝试这样做。将文件内容解析到一个列表中,将死链接保存到另一个列表中,然后使用LINQ扩展名方法Except()
获得差异
这段代码有点凌乱,试图一次完成很多事情。您可能应该将其拆分为更易于管理的部分,如ReadFile()
、CheckLinks()
和WriteFile()
或其他内容
using System.Collections.Generic;
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Linq;
namespace dead_link_finder
{
static class Program
{
private static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
var fileToScan = Console.ReadLine();
var links = File.ReadAllLines(fileToScan);
var deadLinks = new List<string>();
var webClient = new WebClient();
foreach (var link in links)
{
try
{
var content = webClient.DownloadString(link);
if (content.Contains("text-danger"))
{
deadLinks.Add(link);
}
}
catch (WebException wex)
{
if (wex.Status == WebExceptionStatus.NameResolutionFailure )
{
deadLinks.Add(link);
}
}
}
Console.WriteLine();
Console.WriteLine("Found: " + deadLinks.Count + " dead links in the collection.");
Console.WriteLine();
Thread.Sleep(5000);
Console.WriteLine("Removing the dead links, please wait...");
var justTheGoodLinks = links.Except(deadLinks);
File.WriteAllLines(fileToScan, justTheGoodLinks);
Console.WriteLine();
Console.WriteLine("Finished...");
Console.ReadKey(true);
}
}
}
使用System.Collections.Generic;
使用制度;
使用System.IO;
Net系统;
使用系统线程;
使用System.Linq;
命名空间死链接查找器
{
静态类程序
{
私有静态void Main(字符串[]args)
{
Console.ForegroundColor=ConsoleColor.White;
var fileToScan=Console.ReadLine();
var links=File.ReadAllLines(fileToScan);
var deadLinks=新列表();
var webClient=新的webClient();
foreach(链接中的var链接)
{
尝试
{
var content=webClient.DownloadString(链接);
if(content.Contains(“文本危险”))
{
添加(链接);
}
}
捕获(WebException wex)
{
if(wex.Status==WebExceptionStatus.NameResolutionFailure)
{
添加(链接);
}
}
}
Console.WriteLine();
Console.WriteLine(“找到:“+deadLinks.Count+”集合中的死链接”);
Console.WriteLine();
睡眠(5000);
WriteLine(“删除死链接,请稍候…”);
var justTheGoodLinks=链接。除了(死链接);
File.writeAllines(fileToScan,只是goodlinks);
Console.WriteLine();
控制台。WriteLine(“完成…”);
Console.ReadKey(true);
}
}
}
EWW在循环中多次读取和写入完全相同的文件。不要这样做,而是读取一次,循环替换每个链接(仅在内存中工作),然后写下所有的更改。谢谢,我没有注意到这一点。为什么人们会投反对票,如果你能对为什么投反对票的问题留下评论就好了。如果你告诉我们,deadlinks
集合是否真的包含元素,我会是一个更好的问题。另外:在使用调试器之前,不要在这里提问,你的最好的朋友,并总是报告调查结果。对投赞成票,但对没有解决实际问题投反对票。哪个imo根本不存在?(当然,对每一行重新阅读和重写整个文件的错误除外…)