Some time ago, when I wrote the blog post importer for BlogEngine.NET, I wanted to be able to display different text in the ClickOnce application dependant on the user opening it directly from a web page. I did some digging and found out that ClickOnce supports query strings just like a website.

That made it possible to create a link similar to this:

BlogConverter.application?url=http://blog.madskristensen.dk/&username=my username

Enable URL parameters

In your ClickOnce project, right-click the project file in Solution Explorer and click Properties. Select the Publish tab and press the Options… button. Here you need to check the Allow URL parameters to be passed to application checkbox and hit the OK button.

Retrieve the parameters

The next thing was to find out how to retrieve the values of the query strings, because a ClickOnce application does not have an HttpContext so there is no Context.Request.QueryString property.

Instead, you need to retrieve the query strings from the application deployment context. I used this handy method, which returns a NameValueCollection of all the query strings.

using System.Deployment.Application;
using System.Web;
using System.Collections.Specialized;

private NameValueCollection GetQueryStringParameters()
{
  NameValueCollection col = new NameValueCollection();

  if (ApplicationDeployment.IsNetworkDeployed)
  {
    string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
    col = HttpUtility.ParseQueryString(queryString);
  }

  return col;

It’s pretty easy if you know where to look.

A couple of days ago, I had to do some simple date and time validation using regex. The format to validate was yyyy-mm-dd hh:mm. It looks simple enough and like always, I thought I could find the regex on the web, but I couldn’t.

I was under a tight deadline and the date/time validation was not that important, so I made a decision to make the worst regex the world have ever seen. Maybe it’s long, ugly and wrong, but it works just enough to make me use it until I have the time to make a better one.

Warning, do not try this at home!

[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]

The good thing about regex is that they are always ugly, most of them are long and only five people in the world understand them enough to see if they work or not.