Reputation: 113
I have a folder named "DocSamples" on the file system, this folder has 100 HTML files and one cascading style sheet file named "docStyle.css", i want to add a reference to this style sheet <link rel="stylesheet" href="docStyle.css">
to each HTML file in the folder using Console Application (C#).
Any idea on how I can implement this?
Thank you,
Upvotes: 1
Views: 1099
Reputation: 113
Thank you Dieter for your response, much appreciated, i edited your code and used the below code, and it's working fine
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);
}
}
Thanks again.
Upvotes: 0
Reputation: 1142
I would suggest something like:
// 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);
}
Upvotes: 1