How to edit a css file in ASP.NET using C# to change the website design


Tags: c#,asp.net,css

Problem :

I need to make a feature in the website where the admin can change designs of elements like Textbox, header, body and footer and fonts.

I tried string[] lines= File.ReadAllLines(); method to read all lines and File.WriteAllLines() to write all the lines after modifying a particular line.

I have adjusted the css to fit each style of the elements in single line for each.

After File.WriteAllLines method, lines sometimes written in a new line, which will affect all other element styles as it depends on line number.



Solution :

This is what WriteAllLines() does:

using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
    foreach (string str in contents)
    {
        writer.WriteLine(str);
    }
}

If any of the strings in 'contents' contains new line ('\n' or '\r\n') then you'll get extra lines written to the file.

Perhaps instead of using WriteAllLines, you could write your own method, that replaces any '\n' or \r\n' in the output before writing them.

e.g.

using (StreamWriter writer = new StreamWriter(path, false, encoding))
{
    foreach (string str in contents)
    {
        writer.WriteLine(str.Replace("\r","").Replace("\n",""));
    }
}

    CSS Howto..

    How to add a margin to a paragraph?

    How to change text color for link in tr element with CSS

    How to create this CSS border

    How to merge CSS classes?

    How to use inline css styling (style=“vertical-alignment:middle;”) in html5 or