Link to home
Start Free TrialLog in
Avatar of VMthinker
VMthinker

asked on

C# Programming How to utilize method HtmlTextWriter?

Hi there! I have a program that is used to parse out certain strings from a log text file and output the results onto the command line console. The program then converts all the results and output the results into a html report.

The program should operate in the following steps:

1

Gather parse data

2

Convert output results to be shown on html? On Interenet Browser

3

Create report.html file?
May someone please advise on the codes on how to utilize the HtmlTextWriter functions to operate the above steps? Thanks!

The Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI;



namespace Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            // Not sure about the HtmlTextWriter part.
            HtmlTextWriter html = new HtmlTextWriter;

            html.AddAttribute(HtmlTextWriterAttribute.Src, imageValue);
            html.AddAttribute(HtmlTextWriterAttribute.Width, "60");
            html.AddAttribute(HtmlTextWriterAttribute.Height, "60");
            html.AddAttribute(HtmlTextWriterAttribute.Alt, "");

            html.RenderBeginTag(HtmlTextWriterTag.Img);
            html.RenderEndTag();

            // Output the below results?
            html.WriteLine(results);

            System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(@"C:\Test\ntfs9.txt");

            int x = 0;

            int k = 0;

            int folder = 0;

            int files = 0;

            int arrayNum = 0;

            foreach (String r in lines.Skip(1))
            {
                String[] token = r.Split(',');

                String[] datetime = token[0].Split(' ');

                String timeText = datetime[4];

                Double rawSize = Convert.ToDouble(token[1]);

                Double fileSize = ((rawSize / 1024) / 1024);

                String actions = token[2];

                String fileName = token[7];

                if ((Path.GetExtension(token[7]).Contains(".exe") || (Path.GetExtension(token[7]).Contains(".msi"))))
                {

                    Console.WriteLine("The time for this array is: " + timeText);

                    Console.WriteLine(token[7]);

                    Console.WriteLine(actions);

                    Console.WriteLine(fileSize);

                    ProgramFilter(actions, k, fileName);

                    x = 1;

                    Console.WriteLine("================================================");

                }

                else if (fileName.Contains("(deleted)"))
                {
                    Console.WriteLine("The time for this array is: " + timeText);

                    Console.WriteLine("There is a high possibility that the following file from the program was deleted by the system via an installer");

                    Console.WriteLine(token[7]);

                    Console.WriteLine(actions);

                    if (fileSize > 15)
                    {
                        Console.WriteLine("The following file is suspicious due to its file size!");
                    }

                    x = 1;

                    Console.WriteLine("================================================");
                }
            }
        }
    }
}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

The constructor for HtmlTextWritertakes a TextWriterfor a parameter. This TextWriter is where the output  will be written. TextWriter has no constructor you can explicitly call. A simple TextWriter to create would be a StreamWriter--StreamWriter inherits TextWriter. An example follows.
HtmlTextWriter html = new HtmlTextWriter(new StreamWriter("out.html"));

Open in new window

Avatar of VMthinker
VMthinker

ASKER

@Kaufmed So how do I "transfer" all the console output results to be shown into the html format? I have no clue how to use the htmltextwriter....
In my example, the output would be stored in file "out.html", which would be written in the current working directory of your application. Are you saying you want the output printed to the console?

HtmlTextWriter is basically just a wrapper for a TextWriter. It provides you with some specialized functions to write HTML-specific items. TextWriters are just wrappers around streams that that give you specialized functions with regard to interacting with streams (e.g. WriteLine(string input) vs. Write(byte[] input, int offset, int length) ). You can basically point the HtmlWriter (via TextWriter, in this case, StreamWriter) to you target output. If you want to print it to the console, Console.Out is itself a TextWriter, so if you wanted, you could change my example to:
HtmlTextWriter html = new HtmlTextWriter(Console.Out);

Open in new window

@Kaufmed thanks for replying but  where do I have to place the
HtmlTextWriter html = new HtmlTextWriter(new StreamWriter("out.html"));

Open in new window

at which portion of the code?
It should be fine where it is. I am still a bit hazy on what the ultimate output is. Are you saying that you want to print to the console AND generate an HTML report; if so, how do the two outputs differ? In other words, what are you putting into the HTML report?
@Kau nope the ultimate output would be to transfer the console outputs into html

E.g. The report generated would contain the similar outputs in html format. In another words there is no need to output the results in console but just to output the results in the HTML report. So in other words all the output now would be transferred into the HTML report instead of the console.

Could suggest some ways to output all the process into HTML format now instead of the console?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note:  The "style" attribute of the body is a bit different between the source and the code. This is not because of a code flaw; rather I changed my example and forgot to change the code I posted. This is the line that generated the attribute displayed in the screenshot:
html.AddAttribute("style", "background-color: blue; color: white; font-size: xx-large;");

Open in new window

2nd note:  Again an oversight on my part, but you should call the Close() method on the HtmlWriter in order to flush all pending writes to the underlying file. Here is the complete, correct example that matches the screenshots I posted.
static void Main(string[] args)
{
    // Not sure about the HtmlTextWriter part.
    HtmlTextWriter html = new HtmlTextWriter(new StreamWriter("out.html"));

    html.RenderBeginTag("html");
    html.AddAttribute("style", "background-color: blue; color: white; font-size: xx-large;");
    html.RenderBeginTag("body");
    html.RenderBeginTag("table");
    html.RenderBeginTag("tr");
    html.RenderBeginTag("td");
    html.Write("This");
    html.RenderEndTag();    // td
    html.RenderBeginTag("td");
    html.Write("is");
    html.RenderEndTag();    // td
    html.RenderBeginTag("td");
    html.Write("a");
    html.RenderEndTag();    // td
    html.RenderBeginTag("td");
    html.Write("test.");
    html.RenderEndTag();    // td
    html.RenderEndTag();    // tr
    html.RenderEndTag();    // table
    html.RenderEndTag();    // body
    html.RenderEndTag();    // html

    html.Close();
}

Open in new window

@Kaufmed Thanks for your reply! But may I ask how do you convert the console.WriteLine() results into html.WriteLine?  And in which parts of the codes can I place the process at? e.g.

if ((Path.GetExtension(token[7]).Contains(".exe") || (Path.GetExtension(token[7]).Contains(".msi"))))
                {

                    Console.WriteLine("The time for this array is: " + timeText);

                    Console.WriteLine(token[7]);

                    Console.WriteLine(actions);

                    Console.WriteLine(fileSize);

                    ProgramFilter(actions, k, fileName);

                    x = 1;

                    Console.WriteLine("================================================");

                }

Open in new window


A Sample of the above output codes (Codes on the first post):

================================================

The time that this event took place was :  22:53:56

The folder name is : C:/Documents and Settings/All Users/Application Data/VMware/VMware Tools

The MAC is : .a..

There is a high possibility that the following folder was used/opened by the USER

The folder size is : 5.340576171875E-05MB

================================================

The time that this event took place was :  22:53:57

The file name is : C:/Documents and Settings/Aaron/Application Data/Microsoft/VCSExpress/9.0/StartPageCache/MSNews.dat

The MAC is : .a..

There is a high possibility that the following file was used/opened by the USER

The actual file accessed time was : 03:10:39

There is a high probability that the file was used/opened.

The file size is : 0.000711441040039063MB

================================================

Open in new window


Thanks!
@Kaufmed Sorry just 1 more quick question! How do you inherit the "HtmlTextWriter html = new HtmlTextWriter(new StreamWriter(@"C:\test\test.html"));" class to another class?

This is because the " ProgramFilter(actions, k, fileName)" class requires the "html" to use the "html.WriteLine()". The below shows the class but I think that I placed the html method wrongly for the other class as the system is showing me errors.

 public static void MacActions(String actions)
        {
            HtmlTextWriter html; // Does not work.

            HtmlTextWriter html = new HtmlTextWriter(new StreamWriter(@"C:\test\test.html")); // When ran, the program outputs the error that the test.html is being used.

            // Any other ways of importing the "html" from public static void main to here?

            if (actions == "m...")
            {
                html.WriteLine("The file was modified" + "\r\n");
            }

            else if (actions == ".a..")
            {
                html.WriteLine("The file was accessed" + "\r\n");
            }

            else if (actions == "..c.")
            {
                html.WriteLine("The file was changed or renamed or deleted or moved" + "\r\n");
            }

            else if (actions == "...b")
            {
                html.WriteLine("The file was created" + "\r\n");
            }

            else if (actions == "ma..")
            {
                html.WriteLine("The file was modified and accessed" + "\r\n");
            }

            else if (actions == "mac.")
            {
                html.WriteLine("The file was modified, accessed and changed" + "\r\n");
            }

            else if (actions == "ma.b")
            {
                html.WriteLine("The file was modified, accessed and created" + "\r\n");
            }

            else if (actions == "m.c.")
            {
                html.WriteLine("The file was modified and changed" + "\r\n");
            }

            else if (actions == "m.cb")
            {
                html.WriteLine("The file was modified, changed and created" + "\r\n");
            }

            else if (actions == "m..b")
            {
                html.WriteLine("The file was modified and created" + "\r\n");
            }

            else if (actions == ".ac.")
            {
                html.WriteLine("The file was accessed and changed" + "\r\n");
            }

            else if (actions == ".a.b")
            {
                html.WriteLine("The file was accessed and created" + "\r\n");
            }

            else if (actions == ".acb")
            {
                html.WriteLine("The file was accessed, changed and created" + "\r\n");
            }

            else if (actions == "..cb")
            {
                html.WriteLine("The file was changed and created" + "\r\n");
            }

            else if (actions == "macb")
            {
                html.WriteLine("The file was modified, accessed, changed and created" + "\r\n");
            }

            else
            {
                html.WriteLine("The file actions were corrupted" + "\r\n");
            }
        }

Open in new window