Archive for the ‘C#’ Category

PageRank tool walkthrough

I’ve added a video on YouTube to walkthrough using the PageRank checking DLL you can download from my website.

 (by the way, there is no audio).

 

Using the PageRank DLL in your applications (VB.NET)

To complete this walkthrough you’ll need to download the PageRank DLL.  This is included in the PageRank Checker download on the downloads page.

Extract the ZIP file.  The DLL file is in the path: GetPageRank.zip\PageRank\bin\Debug

Make a note of the path as you’ll need it later.

 

Start by opening up Visual Studio (or Developer Express etc) and creating a new project of type: Windows Application.

Then add the following controls to the main form:

Label: Text = “Website URL”

Label: Text = “Google PageRank:”

Label: Name = “lblResult” and Text = “-”

Textbox: Name = “txtURL”

Button: Name = “btnGo” and Text = “GO”

 

The next step is to add a reference to the DLL into your project.  Generally you would right-click on the Project in the Solution Explorer and select “Add Reference”.

When the dialog box eventually opens up, select the “Browse” tab and find the DLL file which should be called “PageRank.dll”, then click OK.

 

OK, double-click on the button you’ve labelled “GO” which will create an event handler for the button’s OnClick event.

I’ve provided the code in C# and VB.Net

Directions for C#

Add the following line to the top of the file:

using PageRank;

Then inside the event handler:

TGooglePR prCheck = new TGooglePR();
lblResult.Text = prCheck.ReturnPageRank(txtURL.Text);

and you’re done.

Directions for VB.Net

Add the following line to the top of the file:

Imports PageRank

Then inside the event handler:

Dim prCheck As New TGooglePR()
lblResult.Text = prCheck.ReturnPageRank(txtURL.Text)

and you’re done.

Just run the project and enter a full URL in the textbox before clicking on “GO”.  The Google PageRank will be shown in the label within a second or so.

Enjoy.

Plus I’ve done a video walthrough in C# and posted it on YouTube: http://www.youtube.com/watch?v=F9MgNVmJnro

Generate XML sitemap for Google

There are many sites that deal with returning a Google sitemap from .NET pages. Most of these need you to adjust the IIS settings (yes this is about Windows hosting).

There are also some that deal with creating a sitemap on-the-fly from the web.sitemap file in your project but here I’ve included the code to return an XML sitemap that conforms to the Sitemap protocol that you can submit to Google without modifying IIS – something that should interest those of you who are on shared hosting.

The ZIP download is available at the bottom of this article.

Basically if you create a blank ASPX page and clear out all the HTML elements from the ASPX page you will just be left with the <% @Page %> definition. Below is an example of the only line that needs to be in the front file (.ASPX).

For your purposes, just add the ContentType=”text/xml” section. It may NOT be necessary once you read through the page-behind code, but I’ve left it in as it doesn’t hurt.

Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMLSiteMap.aspx.cs" Inherits="XMLSiteMap" ContentType="text/xml" %>

Next you will need to put the GSiteMap.cs file in your App_Code folder.

In the page-behind code, you can then simply call the class and all the work is done for you. The code uses the filesystem (whether it is running locally or on a remote server) to generate the sitemap. It will also return the correct protocol type (http or https) and the port number if not on port 80.

I have used this method before the generate an XML file in the filesystem but since my hosting provider doesn’t allow ASPNET to write to the root directory of the site, returning the sitemap on-the-fly is the only truely automated method for this.

In the page-behind’s On_Load event:

protected void Page_Load(object sender, EventArgs e){GSitemap _siteMap = new GSitemap();_siteMap.ProcessRequestFS(Context);}

This simply passes the current HTTPContext to the sitemapping class allowing it to replace the Response with your pure XML sitemap.

I won’t go into the full code at this point because you can read through it yourself from the download. It’s worth pointing out the following however:

private string[] _Allowed_Extensions = { ".aspx", ".php", ".asp", ".htm", ".html", ".txt", ".doc", ".pdf", ".jpg", ".gif", ".xml" };private string[] _Restricted_Directories = { "App_Data", "App_Code", "admin" };

1. Put any extensions you want to be indexed in the “Allowed Extensions” array.

2. Put any directories you don’t want indexed in the “Restricted Directories” array.

Where the code pulls a list of files from each directory I initially used a file pattern, ie:

"*." + Extention

but found that some files were being indexed twice – this is because of a flaw in the framework that will return .ASPX files when you ask for .ASP files. For this reason I re-worked the code. It’s less efficient this way but it’s guaranteed to work.

The call to “ProcessRequestFS” iteratively goes through each directory adding files to the sitemap. If a directory is blocked by the “Restricted Directories” array then all sub-directories of that Directory are also blocked.

You can see an example of the output of this code by visiting: (not currently available)

On my site you may notice that I have temporary removed the optional tags from the sitemap. They are however created in the version available for download.

In particular, the priority tag is automatically down-graded for each directory further down the path that the script has to look.

There is no real error handling in this version but you can add that as necessary.

I checked with Google and Yahoo! and as far as I can see they have no problem with you adding a sitemap with the .ASPX extension.

The full code can be downloaded here: http://www.aaronreynolds.co.uk/page/Code.aspx

The full code is unavailable at the moment and will be online again soon.

If you have any problems using the code, please let me know.

AR

Unable to Evaluate Expression – explanation

Today was actually the first time (that I can remember) I have come across the following error message:

“Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack”

Unfortuntely, today also happened to be a very long day following a very long night (working until 5am last night).

So when this error appeared in a fresh page, based 90% on my previous code I was stumped, angry and frustrated.  Thankfully for me the error was simple to rectify, however it is important to note the error has a purpose rather than it being a bug.

This error crops up when terminating the Response to a page either through Response.End, Response.Redirect or Server.Trasnfer

You may not even notice the error 95% of the time that it occurs.

I had the lethal combination of a try-catch block and a Response.Redirect.  I think I’ll explain the significance of the error first and then the simple work-around.

Basically, the Response object is a .NET wrapped set of code that has a certain life-path.  This path is supposed to end with a Response.End (either explicit or implied) that passes execution to the Application_EndRequest method.  When the correct path is not followed, a ThreadAbortException is thrown.

I moved my Response.Redirect out of a try-catch block and appended the optional “terminate execution” parameter of “false” as follows:

Response.Redirect(“going-somewhere.aspx”, false);

This alleviated my problem and I was able to go back to swearing about something else that wasn’t working as expected.

Microsoft’s explanation and fixes are listed here: ThreadAbortException when using Response.Redirect

C# Version of PR Checker

After a fair bit of trawling around it seems that no-one has created (at at least published) a C# version of the Google PageRank script that is flying around the Internet in PHP form. For those of you who aren't aware of it's existence or purpose: the script automatically generates the necessary URL to find out a page's PR from Google.

In order to stop your average Joe from spamming this facility to bits they put some minor security on it. In order to retrieve the PR score from the query, a hashcode is generated and sent with it.

A few clever chaps noticed how this was being generated and made a PHP script to replicate the process, thus effectively allowing anyone with PHP knowledge to use the Google service.

Now since Google put the protection on the service, should we really be bypassing it?

I have seen many sites hosted in various countries that directly pull PR counts from Google. Most link directories and SEO tools sites have a variation of this in place. Is is legal to do so and does it violate their terms of service?

I don't at this stage know the answer to that so I am not releasing my code (which by the way is C# converted from PHP). My version is based upon the code attributed to the website: http://pagerank.gamesaga.net/

Once I have established the ligitimacy of the original PHP code – as it may have come about through reverse-engineering techniques – I will if possible provide a C# download version.

I also hope to include this facility in the new directory I am building in C#.

Recover FileZilla FTP Passwords

I needed to recover my FTP passwords from a copy of FileZilla today but I either didn’t trust the various downloads or didn’t want to pay to use them.

So here is the free-to-use code for recovering your FileZilla FTP passwords.

You will need to copy the “Pass” variable from the FileZilla.xml file or registry into the textbox when you run. Unlike the other versions out there, this doesn’t go looking for the password files or search the registry. I think you can take care of that, the difficult part of deciphering the password is taken care of for you.

Code as usual is in C#

Download FileZilla Password Recovery Code here

Page Rank tool in VB.Net and C#

A couple of weeks ago another chap named Aaron asked me if I had done a copy of the PageRank code but this time in VB.Net

I said I’d take a look because the versions out on the Internet that he had seen didn’t work. Well this evening I found the time to do the VB.Net version.

I started by manually converting my C# code to VB.Net – bear in mind I haven’t worked in VB.Net for over 2 years now. When the conversion was complete I found that VB.Net was converting and handling integer types slightly differently to C#.

When my code failed to work I resulted to using a free online C# to VB.Net conversion utility on devfusion. The conversion was pretty much word for word what I had put and still didn’t work.

I then went through the process of stepping each iteration and for-next loop until I found the cause of the differences in values being retured. Turns out in the end it was simply the “integer division” operator that I needed to use in place of the standard division operator.

So here it is: go to download pagerank tool code in VB.net and C#

Also bear in mind this is .Net v2 code – I haven’t tested it on previous versions of VB.Net

If you use this in your code or on your website, I’d appreciate you linking back to my website.

Enjoy

Full code for PageRank tool in C#

I’ve uploaded a copy of the code in a demo website for the pagerank tool in C#.

You can download it from: Full PageRank code with demo site

I’ve tried out a few websites and URLs that people have posted to me and got mixed results. I will be updating it if I can find fixes. In particular the hash seems to fail on some sub-directories.

Download it and try it out for yourself. Feedback is always welcome and drop a comment while you’re here.


UPDATE

The previous code was being blocked by Google for some requests due to a mis-calculated hash. This has been fixed now and the download updated.

You can also checkout my online demo at (not currently live)


UPDATE

I’ve been asked by Aaron Brown to provide a VB.net version of this code or at least of the StrToNum part. I’ll take a look at this shortly. Or if you have partly-working code, send it over and I’ll see if I can help out.

Website thumbnailer in C#

After some searching around for ways to implement thumbnails simply for a directory site I am working on I decided to adapt some of the available options into a thumbnailer that I could use.

This is currently in development but when complete will be made available as a downloadable Windows forms app for those who are interested.

Details to follow shortly

Thanks,

AR

C# Google PR Checker

I’ve had some feedback about the google PR checker I have written in C#.

Basically I will be adding a property that allows you to select the datacentre to query and I will be randomising the PR requests where this property is not set.

Additionally I will be adding an automatic double-check facility so if a site ending in a slash doesn’t return a PR it will check again without the slash and vice-versa.

I will post again once the updates are made and post a link to the download.

For those of you wanting to download the current version, you can find it here:PageRank tool in C#

Thanks,

AR

Return top