Category Archives: Development - Page 4

How to run application after VS MSI installer finish

Sometimes you need to do something that is not provided ‘out-of-the-box’ – as provided by the default tools you use. Take Visual Studio’s built in ‘Set-up’ and installation projects as an example.

I wanted to simply add the option to launch the executable that was installed during the install process once the installer ends. Sounds simple and there are ways of doing this with and without Visual Studio using custom installer classes plus ‘Custom Actions’ is possible but it has its drawbacks – like it suspend the installer or you have to write extra code inside your app just to cater for that.

Interestingly enough, MSI installers actually supports this functionality ‘natively’ i.e. it is a built-in feature of MSI technology to have the option for ‘Run my app after the installer ends’. The problem is just that Visual Studio doesn’t expose this functionality itself. The only way to ‘enable’ this feature is to modify the MSI package afterwards – through a ‘Post build action’ or manually (or you can use msbuild or any other build process tool).

To do this you can use a JavaScript script that simply makes the required changes to the MSI package (its internal database). See Aaron Stebner’s blog for more details. The example script to do this is located here.

I initially had trouble getting the working paths correct for my specific project but it turned out the normal ‘build’ command in VS does not always rebuild the solution. Instead you have to explicitly use the ‘Rebuild’ command on the Set-up project to make sure it recreate the MSI using the post build action script.

To summarize how to do it:

  1. First download a copy of the ‘EnableLaunchApplication.js’ script to the set-up projects directory – the directory where the .vdproj file is located.
  2. Then edit the js file to specify the actual exe name of your application you want to ‘launch’ after the MSI finish. You can of course customize it further.
  3. In the Visual Studio set up project’s project properties specify the following ‘PostBuildEvent’: cscript.exe “$(ProjectDir)EnableLaunchApplication.js” “$(BuiltOuputPath)”
  4. Take not that the spelling mistake (BuiltOuputPath) is deliberate since that is how the VS team implemented it (was probably a typo which they never fixed)
  5. Now you can use the ‘Rebuild’ command for the (set-up) project.

and thats how it works. Have fun!

HTMLWriter 1.7

Just a quick refresh on my HTMLWriter utility library. Some minor changes/additions:

1. Added support for the NOBR tag

2. Improved the special character functions including ignoring the ‘&’ sign when needed.

Latest version here.

Java pie

A useful JavaScript library I came across recently is the one created by Balamurugan that is based on the wz_jsgraphics library from walterzorn.

I made some modifications to the Pie library so more ‘properties’ can be specified by the calling script/code which makes it easier to use.

An example:

JavaScript Pie graph

<script type="text/javascript" src="wz_jsgraphics.js"></script>
<script type="text/javascript" src="pie.js"></script>
<div id="pieCanvas" style="overflow: auto; position:relative;height:300px;width:400px;"></div>
<script type="text/javascript">

 

var p = new pie();
p.radius = 100;
p.labelDistance = 120;
p.xCenter = 200;
p.yCenter = 150;
p.add("Jan",49);
p.add("Feb",51);
p.add("Mar", 87);
p.add("Apr",120);
p.add("May",56);
p.add("Jun",41);
p.add("Jul",31);
p.add("Aug",65);
p.fontName = "Tahoma";
p.labelFontSize = 14;
p.render("pieCanvas", "This is a test");

With this you will get a plain (but nice) pie graph that can use customize a bit if you like.

Here is a link to my modified version: JavaScriptPie

Javascript tip: innerText or textContent

Just a quick tip how to handle using an UI object’s innerText property properly. Even today there are differences on how web browsers give you access to the ‘Text’ content of an HTML element. Let say you have the following html:

<p id=”mine”>This is some text</p>

To get the ‘content you must use the following:

IE and Chrome

mine.innerText

but in Firefox

mine.textContent

To make a ‘universal function to get this text you can use the following function:

function getInnerText(obj){
  if (obj){
    if (obj.innerText)
      return obj.innerText;
    else
      return obj.textContent
    }
  else
    return "";
}

Rumours of desktop development death greatly exaggerated

Ok, Microsoft has restored some faith from my side in them – after announcing that the new Visual Studio 11 (now called VS 2012) would NOT support any desktop development anymore.

They eventually changed their minds due to a lot of pressure and decided to release a desktop specific version as well – see this.

A quote from the article:

With this new Express edition, developers will be able to use C++, C#, or Visual Basic to create Windows desktop and console applications. Developers will also get access to new advances available across the Express family in Visual Studio 2012, such as the latest compilers and programming language tools, integrated unit testing, and the ability for small development teams to collaborate via Team Explorer and TFS Express.

Now, we just need to get MS to drop Metro and BioWare to fix Mass Effect 3’s ending… and the world could be a happy place again… hehe

Provide “Export to CSV” functionality on an ASP.Net page

I don’t do (or particularly like) ASP.Net development often but recently had to quickly add some ‘Export to CSV’ functionality of a displayed table to a page. Personally I would have just used ‘copy and paste’ to do it (the export) but since the user isn’t in the habit of thinking for themselves I had to create new functionality which turn out to be very nice – and even opens the door for some future more advanced functionality.

Anyway, to achieve this is really easy if you use the HttpContext.Current.Response class and add an attachment ‘header’ to your page. The header must be named ‘content-disposition‘ and ContentType ‘text/csv‘. See the following methods I created to simply take a DataSet and convert it to ‘CSV file’:

public class CSVExportUtils
{
    public void ExportDataSetToCSV(string title, DataSet ds, string columnNamesCSV = "")
    {
        string attachment = "attachment; filename=" + title.Replace(",", "").Replace(".", "") + ".csv";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "text/csv";
        HttpContext.Current.Response.AddHeader("Pragma", "public");
        if (columnNamesCSV.Length == 0)
        {
            WriteColumnName(ds);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                if (ds.Tables[0].Columns.Count > 0)
                {
                    string value = r[0].ToString();
                    if (value.Contains(","))
                        value = "\"" + value + "\"";
                    HttpContext.Current.Response.Write(value);
                    for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
                    {
                        value = r[i].ToString();
                        if (value.Contains(","))
                            value = "\"" + value + "\"";
                        HttpContext.Current.Response.Write("," + value);
                    }
                }
                HttpContext.Current.Response.Write(Environment.NewLine);
            }
        }
        else
        {
            HttpContext.Current.Response.Write(columnNamesCSV);
            HttpContext.Current.Response.Write(Environment.NewLine);
            string[] columns = columnNamesCSV.Split(',');
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                string value = r[columns[0]].ToString();
                if (value.Contains(","))
                    value = "\"" + value + "\"";
                HttpContext.Current.Response.Write(value);
                for (int i = 1; i < columns.Length; i++)
                {
                    value = r[columns[i]].ToString();
                    if (value.Contains(","))
                        value = "\"" + value + "\"";
                    HttpContext.Current.Response.Write("," + value);
                }
                HttpContext.Current.Response.Write(Environment.NewLine);
            }
        }

        HttpContext.Current.Response.End();
    }
    private static void WriteColumnName(DataSet ds)
    {
        StringBuilder sb = new StringBuilder();
        if (ds.Tables[0].Columns.Count > 0)
        {
            string value = ds.Tables[0].Columns[0].Caption;
            if (value.Contains(","))
                value = "\"" + value + "\"";
            HttpContext.Current.Response.Write(value);
            for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
            {
                value = ds.Tables[0].Columns[i].Caption;
                if (value.Contains(","))
                    value = "\"" + value + "\"";
                HttpContext.Current.Response.Write("," + value);
            }
        }
        HttpContext.Current.Response.Write(Environment.NewLine);
    }
}

This method also allows you to specify the fields you want to export – or alternatively it just use the ones in the DataSet. For example, you can use it like this:

if (Request.QueryString["csvexport"] != null)
{
    CSVExportUtils csvExportUtils = new CSVExportUtils();
    csvExportUtils.ExportDataSetToCSV(fileName, dataSet, "Field1,Field2,...");
}

The calling page can then simply have a hyperlink like this:

SomePage.aspx?csvexport=Yes

Death to desktop development

Well, perhaps that is what Microsoft is trying to achieve with its latest move to remove all desktop development functionality from its Express versions of Visual Studio. Yes, you can still create desktop apps in the ‘pro’ version but at something like $500 it isn’t cheap for hobbyist op someone that is still learning.  Lately they have been making all kind of wild and ridiculous decisions trying to push their new ‘Retro’, eh I mean ‘Metro’ interface down our throats. I for one don’t like this new interface ‘on the desktop’ at all and I’m definitely not the only one!

Just looking at the comments in articles like these you can get the impression that more and more people (developers) are really not liking this new direction that ‘they’ are taking. It is one thing to create something new and exciting but another to actively alienate/irritate your existing followers just to try and push them into a new direction (which might or might not suit them). Is this rush to try and play ‘catch-up’ with ‘the fruit company’ so important that they actually are shooting themselves in the foot? Yes, that is right, they are actively playing the catch-up game but is loosing it with every move they are trying. Not a good move if you want to survive in the end. As big as they are (once) ‘they’ are now in the ‘failing’ game – as Adam Savage would say – Failure is always an option. Microsoft is now ‘living’ that lotto!

Was good knowing ya: RIP Microsoft 1975-2012

Passing a ‘Method’ as parameter in C#

This is actually nothing new but sometimes you might still need a quick reference for to remember how to do this quickly… Say you have a generic class that does something like generating a report and display it somehow (or save or whatever the class do) and want all the ‘calling classes’ to just pass the ‘Method’ that it must ‘run’ in order to generate the report. Yes there are probably nice and fancy tools these days that could also do it if you have the mulla for it…

A solution is to use Anonymous methods that has been with C# (.Net) since version 3 of something.

Reporter r = new Reporter();
r.Report = delegate
{
string stuff = "example report yada yada yada";
return stuff;
}
r.Show();

The generic class that provides the ‘generic’ functionality can then look something like this using a defined delegate so it knows what output it will receive when ‘running’ the method.

public delegate string RefreshActionDelegate();
class Reporter
{
    public RefreshActionDelegate Report { get; set; }

    public Show()
    {
       if (Report != null)
       {
          string theContent = Report();
          DoSomethingWithContent(theContent);
       }
    }
}

This is just a quick example but it shows how you can use the main ‘generic’ class from multiple places each time just passing a different ‘Method’ with its own functionality.

Formatting C# code in WordPress post

If you are like me working with C# a lot and trying to post articles on your WordPress site you might like this: Alex Gorbatchev’s SyntaxHighlighter

Example source code listing

[ sourcecode language=”csharp” ]
void Stuff
{

int moreStuff = 0;

}
[ /sourcecode ]

Would look like this:

void Stuff
{
    int moreStuff = 0;
}

I haven’t tested it with all possible language features but at least it is a start!

Too old to be a ‘software engineer’?

One of the newsletters (emails) I subscribe to recently had an entry about the issue ‘Is Software Engineering Dead?’ – by Steve Jones. This is so true…

Let me ‘copy’ the editorial here:

Is Software Engineering dead? It’s a premise that’s in this article from Bloomberg. It calls the job a career dead end where job prospects decline after one reaches 35 years of age. I think that’s a naive view of the industry, despite the quotes from Craig Barrett and Mark Zuckerberg. While the best of the best might peak in their careers at the same age as many athletes, that doesn’t imply that the career choice is a poor one.

There’s a discussion on Slashdot as well, and I was struck by a few of the comments, which seem to suggest that if you are still just a “software engineer” at age 40, you aren’t very good at your job. It seems that some people seem to think that everyone in this business is constantly looking to move to a new job and advance into management or they aren’t successful . I saw analogies with other industries thinking that same thing, that experienced people would not still be doing the same job after 20 years.

That seems crazy to me. So many people in various industries continue to improve at their jobs, and do the same work for decades. Carpenters, doctors, accountants, all continue to improve their skills over time, and while some may more into management or open their own firms, many will continue to do their same jobs until they retire. And they’ll enjoy their careers.

I don’t deny that ageism exists, and that older programmers often can’t, or won’t, work as many hours as younger ones, but that doesn’t mean they aren’t worth their salaries. Good, experienced programmers produce better code, with fewer bugs. Once managers learn that working more hours doesn’t equal better code, or even more code, the entire industry will start producing better applications.

Steve Jones from SQLServerCentral.com

Well now, without giving away my age too easily.. (cough cough) I’m afraid I’m this piece is very relevant to me too. I’ve been in the software/development/computer/IT industry for many many years (~16+ year) and I still enjoy the basics of software development – but I’ll admit I’m not doing as much as I use to and definitely not as many different things, including new stuff all the time. So is it possible to go on as a ‘software engineer for ages over (say) 35? The perception out there seems to be you have to move into ‘management’ or so in order to ‘progress’ in your career. Trouble is I’m not a ‘peoples’ person and I don’t like managing them either. Is that wrong? Should I be forced to do something I don’t like and probably won’t be good at? Flip-side – Is it wrong to stay with something you like for your whole career? Certainly the market don’t appreciate you – since it doesn’t count how many years experience you have, at a certain point you just don’t get paid any more than before. Personally I think that is an insult and that ‘we’ are worth more not just because of the years of good experiences we have but also the bad things we learned ‘not to’ do. Perhaps there is a place for a more ‘mentoring’ role – which is different from managing a person.

I must admit that lately in my ‘day job’ I’ve been more into ‘administrating’ (BizTalk) than actually developing. This too is sort of just another part of software engineering as it requires me to find clever and original ideas to do my job as other ‘developers’ keep on finding interesting ways of breaking the things I have to administrate. In my personal time in non working hours I still work on ‘pet’ projects mostly around genealogy projects. Some things in life do get better with age – like red wine…

Lets drink to that… (responsibly of course) 😉