Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘.net’

Website Recursive Url Parser

Posted by Jean Paul on March 26, 2011

In this article I am trying to share a piece of code that might be useful to some of the developers.

We can find a lot of code in C# that will parse the http urls in given string. But it is difficult to find a code that will:

  • Accept a url as argument, parse the site content
  • Fetch all urls in the site content, parse the site content of each urls
  • Repeat the above process until all urls are fetched.


Taking the website (A Stock Market Site) as example I would like to get all the urls inside the website recursively.


The main class is SpiderLogic which contains all necessary methods and properties.


The GetUrls() method is used to parse the website and return the urls. There are two overloads for this method.

The first one takes 2 arguments. The url and and a Boolean indicating if recursive parsing is needed or not.

Eg: GetUrls(”, true);

The second one is 3 arguments, url, base url and recursive Boolean.

This method is intended for usage like the url is a sub level of the base url. And the web page contains relative paths. So in order to construct the valid absolute urls, the second argument is necessary.

Eg: GetUrls(“”,, true);

Method Body of GetUrls()

public IList<string> GetUrls(string url, string baseUrl,

bool recursive)


if (recursive)



RecursivelyGenerateUrls(url, baseUrl);

return _urls;



return InternalGetUrls(url, baseUrl);



Another method of interest would be InternalGetUrls() which fetches the content of url, parses the urls inside it and constructs the absolute urls.

private IList<string> InternalGetUrls(string baseUrl, string absoluteBaseUrl)


IList<string> list = new List<string>();

Uri uri = null;

if (!Uri.TryCreate(baseUrl, UriKind.RelativeOrAbsolute, out uri))

return list;

// Get the http content

string siteContent = GetHttpResponse(baseUrl);

var allUrls = GetAllUrls(siteContent);

foreach (string uriString in allUrls)


uri = null;

if (Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out uri))


if (uri.IsAbsoluteUri)


if (uri.OriginalString.StartsWith(absoluteBaseUrl)) // If different domain / javascript: urls needed exclude this check







string newUri = GetAbsoluteUri(uri, absoluteBaseUrl, uriString);

if (!string.IsNullOrEmpty(newUri))






if (!uriString.StartsWith(absoluteBaseUrl))


string newUri = GetAbsoluteUri(uri, absoluteBaseUrl, uriString);

if (!string.IsNullOrEmpty(newUri))





return list;


Handling Exceptions

There is an OnException delegate that can be used to get the exceptions occurring while parsing.

Tester Application

A tester windows application is included with the source code of the article.

You can try executing it.

The form accepts a base url as the input and clicking the Go button it parses the content of url and extracts all urls in it. If you need a recursive parsing please check the Is Recursive check box.


Next Part

In the next part of the article, I would like to create a url verifier website that verifies all the urls in a website. I agree after doing a search we can find free providers like that. My aim is to learn & develop a custom code that could be extensible and reusable across multiple projects by community.

Source Code

The associated source code can be found in c-sharpcorner.  Please search using the same title there.

Posted in ASP.NET | Tagged: , , | 2 Comments »

C#–Invoke CPP method returning String

Posted by Jean Paul on March 16, 2011

public static extern void GetString(ref StringBuilder sb, int length);

StringBuilder sb = new StringBuilder(128);
GetString(ref mySB, 128);

Posted in C# | Tagged: , , | Leave a Comment »

C#–Invoke CPP method returning String

Posted by Jean Paul on March 16, 2011

public static extern void GetString(ref StringBuilder sb, int length);

StringBuilder sb = new StringBuilder(128);
GetString(ref mySB, 128);

Posted in C# | Tagged: , , | Leave a Comment »

How to make ComboBox highlight color to none?

Posted by Jean Paul on February 24, 2011

In some forums I have seen developers wanted to change the selected color (usually Blue) of ComboBox to normal background color. I would like to add the code for following.

Custom Drawing

The method used for accomplishing this is Custom Drawing.


1. Create a new windows application and place a combo box on the form

2. Set the property DrawMode to OwnerDrawVariable

3. On the combo box DrawItem event, add the following code:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)


ComboBox combo = sender as ComboBox;

e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds);

e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font, new

SolidBrush(combo.ForeColor), new Point(e.Bounds.X, e.Bounds.Y));


Testing the Application

You can add some test data items to the combox and run the application.


Now you can see that eventhough the mouse is on the dropdown list area the highlight color is not there.

Posted in WinForms | Tagged: , , | Leave a Comment »

What is Foreground thread? / What is Background thread?

Posted by Jean Paul on February 24, 2011



Threading is a concept we all knows and most of us would have been implemented it in the real world applications. I would like to inform about one more inner level detail on threads – that is Foreground and Background threads.

Types of Threads

Basically, there are two types of threads which fall into:

  • Foreground Thread
  • Background Thread

Foreground Thread

Foreground threads are threads which will continue to run until the last foreground thread is terminated. In another way, the application is closed when all the foreground threads are stopped.

So the application won’t wait until the background threads are completed, but it will wait until all the foreground threads are terminated.

By default, the threads are foreground threads. So when we create a thread the default value of IsBackground property would be false.

Background Thread

Background threads are threads which will get terminated when all foreground threads are closed. The application won’t wait for them to be completed.

We can create a background thread like following:

Thread backgroundThread = new Thread(threadStart);

backgroundThread.IsBackground = true;


Test Application

We can see the difference by using a windows or console application. In the attachment, I have provided such an application. The application is explained below:


The application contains 2 buttons, on click of each it will create a thread.

Each thread will be executing a method which takes 25 seconds to execute through a mere Thread.Sleep() invoke.

private void TenSecondsMethod()


// Method of 25 seconds delay



The first one will be Background thread and second would be Foreground.

After clicking each button, you can try closing the application.

From the windows task manager we can see the application would not be listed in the case of Background thread created.

From the windows task manager we can see the application would be still running in the case of Foreground thread created.

The following image shows the snapshot of task manager just after closing the application after invoking a foreground thread. The application will continue to run until all the foreground threads are completed.


Recommended Usage

We can specify foreground threads for business critical tasks.

Meantime, the background threads can be useful for polling services or logging services which could be discontinued once the application is closed.

Posted in C# | Tagged: , , | 2 Comments »

When to use lock?

Posted by Jean Paul on February 19, 2011

The lock keyword in C# provides good control over a multi-threaded application.

But if we are using it in an unwanted scenario it could impact performance. Sometimes, the usage of threading itself would not be benefited.

I am trying to explain a valid scenario where lock is advised in a multi-threaded application.

Lock while writing to a file – Advisable

In a multithreaded environment, if a particular method is writing to a file, it is advisable to use lock – otherwise exception will occur.

private void WriteToFile_Locked()


lock (this)


StreamWriter streamWriter = File.AppendText("File.txt");

streamWriter.WriteLine("A line of text..");




Lock while fetching a network resource – Not Advisable

Suppose in a method which is relying on a network resource like copying a file over a network or downloading a file from internet. In this case the lock would degrade the performance as all the threads will be waiting to enter the critical section.

If we use lock in the above scenario we will not be gaining from the threading advantage.

private void ReadNetworkResource()


WebClient client = new WebClient();

var result = client.DownloadData(";);


If we implement lock to the above method it will degrade the performance.

Test Application

A test application is provided to check the performance in both the above scenarios.


The application contains buttons which are self explanatory. The first row of buttons deals with the file issue and it is solved by implementing the lock keyword.

The second row of buttons deal with the network resource issue where lock is implemented and performance was degraded. On clicking the buttons we can see the time difference it is taking to perform the actions.

In the test machine it took 10 seconds for the first one and 25 seconds for the second.

This shows that if lock is not wisely used it could impact the performance in a multi threaded application.

Without Lock

With Lock

10 seconds

25 seconds

About the Source

The source for the above application is attached with the article.

Posted in C# | Tagged: , , | Leave a Comment »

How to find the configuration path of the exe in .Net?

Posted by Jean Paul on February 10, 2011


While on debugging use the following


string path = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None).FilePath

Posted in C# | Tagged: , , | Leave a Comment »

How to avoid security error in ASP.NET textbox while inputting html content?

Posted by Jean Paul on February 9, 2011

The ASP.NET handler checks for security threats on page submissions.  This is part of the security checks, otherwise malicious scripts could be injected to the server.

But, in some alternative cases we need to bypass this – say we need to save a webpage into the database.  But the page validation throws the error.

You can try the following:

1. Create a web application and place a textbox and button on it.

2. Run the application and try entering the following data into the textbox

<html>Test Content</html>

3. Click the button to submit the page

4. You will be receiving the following error:

Server Error in ‘/’ Application.

A potentially dangerous Request.Form value was detected from the client (TextBox1=”<html>test</html>”).

Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. You can disable request validation by setting validateRequest=false in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case.
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (TextBox1=”<html>Test Content</html>”).



Set the ValidateRequest property to false in the page level

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”WebApplication2._Default” ValidateRequest=”false” %>

Now try rerunning the application and click the button.  The error is disappeared now.

Posted in ASP.NET | Tagged: , , | 2 Comments »

How to view disassembly code in VS 2005/2008?

Posted by Jean Paul on February 7, 2011

I found that some of the community members are relying on third party tools to view the disassembly generated for .Net applications.  It would be nice to see that already this feature is supported by Visual Studio 2005/2008.

In the following application I am trying to show the disassembly generated.

Step 1: Create a console application

Create a new console application and replace the main method with the following code.

    static void Main(string[] args)
        int i = 10;
        int j = 20;

        int sum = i + j;

        Console.WriteLine(string.Format(“Sum is: {0}”, sum));

Step 2: Place a breakpoint at the end of Main method()

It is depicted in the image below.


Step 3: Execute the application

Press F5 to debug the application, and when control reaches the breakpoint, right click on the code and from the context menu choose  “Go to Disassembly”


You can see the following output:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisassemblyApp
    class Program
        static void Main(string[] args)
00000000  push        ebp 
00000001  mov         ebp,esp
00000003  sub         esp,20h
00000006  mov         dword ptr [ebp-4],ecx
00000009  cmp         dword ptr ds:[04D2707Ch],0
00000010  je          00000017
00000012  call        5F311579
00000017  xor         edx,edx
00000019  mov         dword ptr [ebp-10h],edx
0000001c  xor         edx,edx
0000001e  mov         dword ptr [ebp-0Ch],edx
00000021  xor         edx,edx
00000023  mov         dword ptr [ebp-8],edx
00000026  nop             
           int i = 10;
00000027  mov         dword ptr [ebp-8],0Ah
           int j = 20;
0000002e  mov         dword ptr [ebp-0Ch],14h

            int sum = i + j;
00000035  mov         eax,dword ptr [ebp-8]
00000038  add         eax,dword ptr [ebp-0Ch]
0000003b  mov         dword ptr [ebp-10h],eax

            Console.WriteLine(string.Format(“Sum is: {0}”, sum));
0000003e  mov         ecx,62D708h
00000043  call        FA13826C
00000048  mov         dword ptr [ebp-14h],eax
0000004b  mov         eax,dword ptr ds:[02AF8370h]
00000051  mov         dword ptr [ebp-1Ch],eax
00000054  mov         eax,dword ptr [ebp-14h]
00000057  mov         edx,dword ptr [ebp-10h]
0000005a  mov         dword ptr [eax+4],edx
0000005d  mov         eax,dword ptr [ebp-14h]
00000060  mov         dword ptr [ebp-20h],eax
00000063  mov         ecx,dword ptr [ebp-1Ch]
00000066  mov         edx,dword ptr [ebp-20h]
00000069  call        dword ptr ds:[001F8E20h]
0000006f  mov         dword ptr [ebp-18h],eax
00000072  mov         ecx,dword ptr [ebp-18h]
00000075  call        dword ptr ds:[04816794h]
0000007b  nop             
0000007c  nop             
0000007d  mov         esp,ebp
0000007f  pop         ebp 
00000080  ret             

Posted in C# | Tagged: , , | Leave a Comment »

How to send Html Email using c#?

Posted by Jean Paul on February 7, 2011

SmtpClient client = new SmtpClient(“”);
client.Credentials = new NetworkCredential(“”, “password”);

MailMessage message = new MailMessage(“”,
                               “Testing Html Email”,
                               “<h1>Your Email Content</h1>”);

message.IsBodyHtml = true;

Posted in C# | Tagged: , , | Leave a Comment »