Hi All

I have a small app. What is does it reads an html file for certain keywords (like e9, e5, e21, etc.) When any of those lines are found it must add a link in html format into the file. Except you will see that the folder locations differ.

For example, the folder structure is Test Images, inside ther are folders called e2, e5, etc. In them are images. When the link is created it must automaticaly input the correct location. Say if e2 is found then the link must be like this:

<a href='Test Images\e2'><img src='Camera.png' style='position: absolute; top: 185; left: 1050;' border='0' alt='Hello'></a></map></body></html> (Note the e2)

Below is the code I have so far, so can anyone help me perhaps?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace TrueViewApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fName = @"Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm";//path to text file
            StreamReader testTxt = new StreamReader(fName);
            string allRead = testTxt.ReadToEnd();//Reads the whole text file to the end
            testTxt.Close(); //Closes the text file after it is fully read.
            string regMatch = @"e";//string to search for inside of text file. It is case sensitive.

            if (Regex.IsMatch(allRead, regMatch))//If the match is found in allRead
            {
                StreamWriter SW;
                SW = File.AppendText("Y:\\WorleyParsons\\Sasol Unit 17\\Submission Data\\Tru-View\\Unit17TruViewInterface\\TruView\\Unit17\\SiteMap.htm");
                SW.WriteLine(@"<a href='Test Images\e3'><img src='Camera.png' style='position: absolute; top: 185; left: 1050;' border='0' alt='Hello'></a></map></body></html>");
                SW.Close();
                MessageBox.Show("Line successfully Appended");
                Application.Exit();
            }
            else
            {
                MessageBox.Show("not found");
            }
        }
    }
}

Recommended Answers

All 22 Replies

Instead of using that if statement:

if (Regex.IsMatch(allRead, regMatch))

I would suggest you, before doing a loop create an array of all characters (all the words in that string) :

string allRead = testTxt.ReadToEnd();
string allChars = allRead.Split(' ');
//create a new string wtih htmls:
string newString  = null;
//your html string:
string htmlString = "Y:\\WorleyParsons\\Sasol Unit 17\\Submission Data\\Tru-View\\Unit17TruViewInterface\\TruView\\Unit17\\SiteMap.htm";
StringBuilder sb = new StringBuilder();

for(int i = 0; i < allChars.Lenght; i++)
{
    //here do the Regex, or simple string Contains method checking
    //if the char is your tag, add the html link:
    //add the char:
    sb.AppendText(allChars[i].ToString);
    //and add the html link if "the e" is found:
    if(allChars[i].Contains("e"))
        sb.AppendText(htmlString); 
}
//add newly created string from StringBuilde to a "newString" variable:
string newString = sb.Tostring();

This code i did heart, so there can be errors. I appologize for that.
But this is an example code how you can add your string into string.

Okay so I've tuned it a bit, but getting no result?

Thanks for the reply though...

string fName = @"Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm";//path to text file
            StreamReader testTxt = new StreamReader(fName);
            string allRead = testTxt.ReadToEnd();
            string allChars = allRead.Trim(' ');
            //create a new string wtih htmls:
            string newString = null;
            //your html string:
            string htmlString = "Y:\\WorleyParsons\\Sasol Unit 17\\Submission Data\\Tru-View\\Unit17TruViewInterface\\TruView\\Unit17\\SiteMap.htm";
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < allChars.Length; i++)
            {
                //here do the Regex, or simple string Contains method checking
                //if the char is your tag, add the html link:
                //add the char:
                sb.Append(allChars[i].ToString());
                //and add the html link if "the e" is found:
                if (allChars[i].Equals("e"))
                    sb.Append(htmlString);
            }
            //add newly created string from StringBuilder to a "newString" variable:
            newString = sb.ToString();

you want to append htmlString in string builder if you find "e" in any of the line of the file "Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm" and if you find 10 lines which are "e" then you append this htmlString 10 times it that right?

That's correct :)

Except the string will be :

<a href='Test Images\e3'><img src='Camera.png' style='position: absolute; top: 185; left: 1050;' border='0' alt='Hello'></a></map></body></html>

mean if the line contain "\e" with some other characters? then change this line

if (allChars[i].Equals("e"))

to

if (allChars[i].Contains("\\e"))

another way is to write as

string fName = @"Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm";//path to text file
           StreamReader reader = new StreamReader(fname)
            string newString = null;
            //your html string:
            string htmlString = "Y:\\WorleyParsons\\Sasol Unit 17\\Submission Data\\Tru-View\\Unit17TruViewInterface\\TruView\\Unit17\\SiteMap.htm";
            StringBuilder sb = new StringBuilder();
        while ((S = reader.ReadLine()) != null)
        {
            if (S.Contains("\\e"))
                sb.Append(htmlString);
        }

That is the file location. Here is the line inside my htm file:

<html>
<head>
<title>Site Map</title>
</head>
<body>
Here is the site map:<br>
<img src="SiteMap.png" usemap="#TruView">
<map name="TruView">
<area title="e21" shape="circle" coords="837,730,8" href="e21\TruView.xml">
<area title="e20" shape="circle" coords="1030,670,8" href="e20\TruView.xml">
<area title="e19" shape="circle" coords="1046,611,8" href="e19\TruView.xml">
<area title="e18" shape="circle" coords="822,633,8" href="e18\TruView.xml">
<area title="e17" shape="circle" coords="822,534,8" href="e17\TruView.xml">
<area title="e16" shape="circle" coords="975,521,8" href="e16\TruView.xml">
<area title="e15" shape="circle" coords="1040,459,8" href="e15\TruView.xml">
<area title="e14" shape="circle" coords="921,377,8" href="e14\TruView.xml">
<area title="e12" shape="circle" coords="784,210,8" href="e12\TruView.xml">
<area title="e10" shape="circle" coords="790,321,8" href="e10\TruView.xml">
<area title="e9" shape="circle" coords="982,363,8" href="e9\TruView.xml">
<area title="e5" shape="circle" coords="973,249,8" href="e5\TruView.xml">
<area title="e3" shape="circle" coords="1016,131,8" href="e3\TruView.xml">
<a href="Test Images\e21"><img src="Camera.png" style="position: absolute; top: 185; left: 1012;" border="0" alt="Hello"></a>
<a href='Test Images\e20'><img src='Camera.png' style='position: absolute; top: 185; left: 1050;' border='0' alt='Hello'></a>
</map>
</body>
</html>

Look at the area title part. If it finds e21, or any of those under it, it should add the links like I've already done manually.

Have you checked the above code now?

I've looked at it but it read my file location. Check the html code above. It must read those lines..?

Sorry for the vague answers

stream reader reads the contents of a file not the location...I have Checked the following code and it is exactly doing what you told me .... i made "abc.html" file on D drive containing the html code above

string fName = @"D:\abc.html";//path to text file
          StreamReader reader = new StreamReader(fName);
            string newString = null;
            //your html string:
            string htmlString = "D:\\abc.html";
            StringBuilder sb = new StringBuilder();
            String S;
        while ((S = reader.ReadLine()) != null)
        {
            if (S.Contains("\\e"))
            {
                sb.Append(htmlString);
                MessageBox.Show(S);
            }
        }
        MessageBox.Show(sb + "");
        }

ahhh nice work :)

now how can I print a new href line every time an 'e' is found?

ahhh nice piece of code :) thanks!!

Now how can I insert a new line of href into the file every time 'e' is found?

You want to print the line which contains "\e"? I couldn't understand your question please elaborate

I want to print this line everytime 'e' is found:

<a href="Test Images\e21"><img src="Camera.png" style="position: absolute; top: 185; left: 1012;" border="0" alt="Hello"></a>

Did I explain right now? :P

:D yes you explained it right and dear if you closely pay attention to what code i have pasted yesterday prints the line you wanted also..... Should i do :P now?

string fName = @"D:\abc.html";//path to text file
          StreamReader reader = new StreamReader(fName);
            string newString = null;
            //your html string:
            string htmlString = "D:\\abc.html";
            StringBuilder sb = new StringBuilder();
            String S;
        while ((S = reader.ReadLine()) != null)
        {
            if (S.Contains("\\e"))
            {
                sb.Append(htmlString);
                [B]MessageBox.Show(S);[/B]
            }
        }
        MessageBox.Show(sb + "");
        }

Yes that part I do get, but I want to print/insert that line into the htm file...

I know it should be some kind of for loop and then some stream writer...?

sorry for teasing i got your point now check this code

string fName = @"D:\abc.html";//path to text file
          StreamReader reader = new StreamReader(fName);
            string newString = null;
            //your html string:
            string htmlString = "D:\\abc.html";
            StringBuilder sb = new StringBuilder();
            String S;
            sb.Append(htmlString);
            sb.AppendLine();
        while ((S = reader.ReadLine()) != null)
        {
            sb.Append(S);            
           // sb.AppendLine();
            if (S.Contains("\\e"))
            {
                sb.Append(S);
                //MessageBox.Show(S);
            }
            //sb.AppendLine();
        }
        reader.Close();
       // MessageBox.Show(sb + "");
       StreamWriter writeFile = new StreamWriter(@"D:\abc.html");
        
             writeFile.Write(sb.ToString());

Thanks for the private message, looked at the code and modified it a bit. but I get the error at the streamWriter part:

Access to the path 'Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm' is denied.

This is my code:

string fName = @"Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm";//path to text file
            StreamReader reader = new StreamReader(fName);
            //your html string:
            string htmlString = "Y:\\WorleyParsons\\Sasol Unit 17\\Submission Data\\Tru-View\\Unit17TruViewInterface\\TruView\\Unit17\\SiteMap.htm";
            StringBuilder sb = new StringBuilder();
            String S;
            sb.Append(htmlString);
            sb.AppendLine();
            while ((S = reader.ReadLine()) != null)
            {
                //sb.Append(S);
                if ((S.Contains("e21")) || (S.Contains("e20")) || (S.Contains("e19")) || (S.Contains("e18")) || (S.Contains("e17")) || (S.Contains("e16")) || (S.Contains("e15")) || (S.Contains("e14")) || (S.Contains("e12")) || (S.Contains("e10")) || (S.Contains("e9")) || (S.Contains("e5")) || (S.Contains("e3")))
                {
                    sb.Append(S);
                }
            }
            reader.Close();
            MessageBox.Show(sb + "");
            StreamWriter outfile = new StreamWriter(@"Y:\WorleyParsons\Sasol Unit 17\Submission Data\Tru-View\Unit17TruViewInterface\TruView\Unit17\SiteMap.htm");
            outfile.Write(sb.ToString());

Okay managed to get it right, but I noticed that it doesn't add line breaks.

It sort of rewrites the file, instead of just adding those line under the existing lines.

Perhaps I'm just being stupid, don't know :P

it is working for me very fine i just added outfile.Close(); at the end please change the location of file and then check probably the location is unsafe or program don't have permissions to change the contents at this location

Also use sb.AppendLine() instead of sb.Append() just to append each string in a separate line

to append new lines in existing lines please uncomment line number 11 :D and if your problem is solved please mark this thread solved

Thanks for all your help :) really appreciate it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.