C# 如何将CSS引用链接添加到文件夹中的本地HTML文件?

C# 如何将CSS引用链接添加到文件夹中的本地HTML文件?,c#,html,css,console-application,C#,Html,Css,Console Application,我在文件系统上有一个名为“DocSamples”的文件夹,这个文件夹有100个HTML文件和一个名为“docStyle.css”的层叠样式表文件,我想使用控制台应用程序(C#)向文件夹中的每个HTML文件添加对此样式表的引用 你知道我该怎么做吗 谢谢你,我建议如下: // Get all files string filepath = Environment.GetFolderPath("Filepath here"); DirectoryInfo d = new DirectoryInfo(f

我在文件系统上有一个名为“DocSamples”的文件夹,这个文件夹有100个HTML文件和一个名为“docStyle.css”的层叠样式表文件,我想使用控制台应用程序(C#)向文件夹中的每个HTML文件添加对此样式表的引用

你知道我该怎么做吗


谢谢你,

我建议如下:

// Get all files
string filepath = Environment.GetFolderPath("Filepath here");
DirectoryInfo d = new DirectoryInfo(filepath);

//Handle each file
foreach (var file in d.GetFiles("*.html"))

    // Get all text from 1 file
    string readText = File.ReadAllText(file.FullName);
    // Add css
    readText = readText.Replace("</head>", @"<link rel="stylesheet" href="docStyle.css"></head>")

    // Save file with modifications
    File.WriteAllText(file.FullName, readText);
}
//获取所有文件
字符串filepath=Environment.GetFolderPath(“此处的filepath”);
DirectoryInfo d=新的DirectoryInfo(filepath);
//处理每个文件
foreach(d.GetFiles(“*.html”)中的var文件)
//从1个文件中获取所有文本
string readText=File.ReadAllText(File.FullName);
//添加css
readText=readText。替换(“,@”)
//保存修改后的文件
File.writealText(File.FullName,readText);
}

感谢迪特尔的回复,非常感谢,我编辑了您的代码并使用了下面的代码,效果很好

    static void Main(string[] args)
        {
            string[] htmlFiles = Directory.GetFiles("systemDrive\\Doc samples", "*.html");


//Handle each file
foreach (var htmlFile in htmlFiles)
{
    // Get all text from 1 file
    string readText = File.ReadAllText(htmlFile);
    // Add css
    readText = readText.Replace("</head>", @"<link rel='stylesheet' href='docStyle.css'></head>");

    // Save file with modifications
    File.WriteAllText(htmlFile, readText);
}
        }
static void Main(字符串[]args)
{
字符串[]htmlFiles=Directory.GetFiles(“systemDrive\\Doc samples”,“*.html”);
//处理每个文件
foreach(htmlFile中的var htmlFile)
{
//从1个文件中获取所有文本
字符串readText=File.ReadAllText(htmlFile);
//添加css
readText=readText.Replace(“,@”);
//保存修改后的文件
writealText(htmlFile,readText);
}
}

再次感谢。

您好,穆罕默德,我建议使用文件阅读器,并在每个文件上循环。然后,对每个文件执行一个字符串filetext=file.ReadAllText(fileName);。然后做一个filetext.replace(“,”)。当然,之后再保存文件:)不客气。请注意,不能在同一个文件上运行两次,否则将引用css两次。