The life and times of this developer...

WPF, C#, XML and other stuff

  Home  |   Contact  |   Syndication    |   Login
  12 Posts | 0 Stories | 6 Comments | 0 Trackbacks

News

Twitter












Archives

Tuesday, January 31, 2012 #

Hi, it's really handy to be able to add Trace to your .Net application using the System.Diagnostics.Trace class.

The only problem is how the heck do you get it to Trace out at runtime?

The simple solution is to add this to your app config file...

  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <clear/>
        <add name="textwriterListener" type="System.Diagnostics.TextWriterTraceListener"
          initializeData="c:\temp\outfile.txt" traceOutputOptions="ProcessId, DateTime"/>
      </listeners>
    </trace>
  </system.diagnostics>

#

 


Thursday, December 01, 2011 #

After clicking a YouTube video link, I was presented with the helpful message ‘You need to upgrade your adobe flash player to watch this video’ along with a link to Adobe to do just that.

After a long and frustrating cycle of install/uninstall Flash Player, I finally realised that this was a bogus message and should instead have said…

‘Looks like you have accidentally clicked Tools->ActiveX Filter option in IE9!’

Arrggghh…hope this helps someone else out who is getting this message (or me again if I misplace another mouse click).

#

 


Thursday, September 22, 2011 #

I may be a bit slow on the uptake here, but I'm not sure how excited to get about my social networks influencing my search results?

I think it's good that you can indicate you like something on Facebook as your friends can have a look at the item or choose to ignore it, and I guess it makes sense that if you like something, then some of your friends will like the same thing.

But when I do a Google search, do I *really* want it to show bias towards things my friends have liked? Also, how many clicks does an item have to get before it is seen as noteworthy for others? Isn't the pool going to be too small?

Either way, good or bad, as a reaction to the noise about this stuff I was tasked with adding the +1 button to a site. This was painless enough...the official Google instructions can be found here:

http://www.google.com/webmasters/+1/button/

I will now monitor the clicks, but somehow I don't think the count will go up that swiftly!!

#

 


Tuesday, March 15, 2011 #

I have had to rebuild my Windows 7 PC and all has gone fairly well until I tried to connect to a Samba share on a legacy Linux box running Redhat 8.

No matter what combination of domain / user /password I would just see the same message of:
"The specified network password is not correct."

This is a misleading error, very annoying and a little confusing until I found a hint that Windows 7 default authentication was not supported on older Samba implementations.

I guess I figured this out once before as it used to work before the rebuild! Anyway here is the solution:

1. Control Panel->System and Security->Administrative Tools->Local Security Policy (or run secpol.msc).
2. Select Local Policies->Security Options->Network security: LAN Manager authentication level.
3. Select 'Send LM and NTLM - use NTLMv2 session security if negotiated' and click OK.

#

 


Tuesday, March 08, 2011 #

I spent most of yesterday removing an annoying virus from my PC. I feel slightly foolish for getting one in the first place, but after so many years I guess I was always going to eventually succumb. I was also a little surprised at the failure of various tools at removing it.

The virus would redirect the browser to websites including ‘licosearch’, ‘hugosearch’ and ‘facebook’, and the disk would be thrashing away infecting dlls in some way.

I had the full up to date version of McAfee installed. This identified that there was an issue in some dlls on the system and was able to ‘fix’ them. But they kept getting re-infected. So I installed Microsoft Security Essentials and this too was able to identify and ‘fix’ the infected dlls.

The system scans take forever and I really expected better results. I also tried Malwarebytes, Hitman Pro, AVG and Sophos to no avail.

Eventually I thought I’d investigate myself. It turned out that on reboot, the virus would start 3 instances of Firefox.exe which I’m guessing would do bad things including infecting as many dlls on the system as possible.

I removed Firefox and the virus cleverly then launched 3 instances of Chrome! So I uninstalled Chrome and yes, it then started to launch 3 instances of iexplore.exe. If I’m honest, by this stage I was just seeing if it would be able to use any of the browsers!

As it was starting these on reboot, I looked in my User Startup folder and there was a <randomly named>.exe and several log files. I deleted these and rebooted. When I looked they had been recreated. So I then looked in the registry Run and RunOnce entries: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. Sure enough there were entries to run a file in C:\Program Files\<random name folder>\<random name file>.exe.

I deleted this and rebooted and it was fixed. I also looked in the event log and found a warning that Winlogon had failed to start the file C:\Program Files\<random name folder>\<random name file>.exe

So I also checked HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon and this entry had also been changed.

Finally I ran a full system scan to clean up any infected dlls. I hope it’s gone for good!

[UPDATE]

Unfortuately this is the ramnit virus and there are too many infected files to trust that it will be gone for good, so only one solution...format hard drive and start again.

Note: reports about w32.ramnit.a / w32.ramnit.b say that only .dlls, .exes and .html files are compormised, however, my svn source cotrol is showing .doc and .xls files have also changed.

Fortunately I have recent backups which I feel slightly *smug* about as my work colleagues have been less than sympathetic!

 #

 


Friday, March 04, 2011 #

Looking in Web Analytics I could see several external sites pointing at an old .htm file on my web server that no longer existed, so I thought I would get IIS to redirect to the new .aspx replacement. How hard could it be?

This has annoyed me for quite a while today so here is the answer.

1. Install the Http Redirection module - this is not installed by default!!

Windows 7
Start->Control Panel->Programs and Features->Turn Windows Features on or off.
Internet Information Services->World Wide Web Services->Common Http Features->HTTP Redirection.

Windows Server 2008
Start->Administrative Tools->Server Manager.
Roles->Web Server (IIS).
Role Services->Add Role Services.
Common Http Features->HTTP Redirection.

2. Edit your web.config file

<configuration>
    .....
    <location path="oldfile.htm">
        <system.webServer>
            <httpRedirect enabled="true" destination="/newfile.aspx" exactDestination="true" childOnly="true" httpResponseStatus="Permanent" />
        </system.webServer>
    </location>
    .....
</configuration>

When a user clicks or Google crawls ‘oldfile.htm’ it will get a permanent redirect to ‘/newfile.aspx’ - and should take any Page Rank to the new file.

 #

 


Monday, February 28, 2011 #

Microsoft provide an article of the same name (previously published as Q319401) and it shows a nice class 'ListViewColumnSorter ' for sorting a standard ListView when the user clicks the column header.

This is very useful for String values, however for Numeric or DateTime data it gives odd results. E.g. 100 would come before 99 in an ascending sort as the string compare sees 1 < 9. So my challenge was to allow other types to be sorted. This turned out to be fairly simple as I just needed to create an inner class in ListViewColumnSorter which extends the .Net CaseInsensitiveComparer class, and then use this as the ObjectCompare member's type.

Note: Ideally we would be able to use IComparer as the member's type, but the Compare method is not virtual in CaseInsensitiveComparer , so we have to create an exact type:

public class ListViewColumnSorter : IComparer
{
    private CaseInsensitiveComparer ObjectCompare;
    private MyComparer ObjectCompare;

    ... rest of Microsofts class implementation...
}

Here is my private inner comparer class, note the 'new int Compare' as Compare is not virtual, and also note we pass the values to the base compare as the correct type (e.g. Decimal, DateTime) so they compare correctly:

private class MyComparer : CaseInsensitiveComparer
{
    public new int Compare(object x, object y)
    {
        try
        {
            string s1 = x.ToString();
            string s2 = y.ToString();
 
            // check for a numeric column
            decimal n1, n2 = 0;
            if (Decimal.TryParse(s1, out n1) && Decimal.TryParse(s2, out n2))
                return base.Compare(n1, n2);
            else
            {
                // check for a date column
                DateTime d1, d2;
                if (DateTime.TryParse(s1, out d1) && DateTime.TryParse(s2, out d2))
                    return base.Compare(d1, d2);
            }
        }
        catch (ArgumentException) { }
 
        // just use base string compare
        return base.Compare(x, y);
    }
}

You could extend this for other types, even custom classes as long as they support ICompare.

Microsoft also have another article How to: Sort a GridView Column When a Header Is Clicked that shows this for WPF, which looks conceptually very similar. I need to test it out to see if it handles non-string types.

#


Wednesday, February 23, 2011 #

The reason I pose this question is that I'm looking at WPF development and so using the latest version seems sensible. However, this means rolling out the .NET 4 runtime to PCs on old versions of the framework.

Windows XP is still the number one O/S (estimated 40%+ market share). To run .NET 4 on XP requires Service Pack 3, and although it is good practice to move to the latest service packs, often large companies are slow to keep up due to the extensive testing involved.

In fact, .NET 4 is not installed as standard with any Windows O/S as yet - Windows 7 and 2008 Server R2 have 3.5 installed.

This is not quite as big an issue as it was for .NET 3.5 as .NET 4 is significantly smaller as it doesn't include the older runtimes - .NET 3.5 SP1 included .NET 3 and .NET 2 and was 250MB, although this was reduced by doing a web install.

The size is also reduced a bit if you target the .NET 4 Client Profile, which should be OK for many WPF applications, and I think this may be rolled out as part of Windows service packs soon.

But still, if your application is only 4-5 MB and you need 40-50 MB of Framework it is worth consideration before jumping in and using the new shiny features.

#


Monday, February 21, 2011 #

T-SQL has never been my favorite language, but I need to use it on a fairly regular basis and every time I seem to Google the same things. So if I add it here, it might help others with the same issues, but it will also save me time later as I will know where to look for the answers!!

1. How do I SELECT FROM WHERE to filter on a DateTime column?

As it happens this is easy but I always forget. You just put the DATE value in single quotes and in standard format:

SELECT StartDate FROM Customer WHERE StartDate >= '2011-01-01' ORDER BY StartDate

2. How do I then GROUP BY and get a count by StartDate?

Bit trickier, but you can use the built in DATEADD and DATEDIFF to set the TIME part to midnight, allowing the GROUP BY to have a consistent value to work on:

SELECT DATEADD (d, DATEDIFF(d, 0, StartDate),0) [Customer Creation Date], COUNT(*) [Number Of New Customers]
FROM Customer
WHERE StartDate >= '2011-01-01'
GROUP BY DATEADD(d, DATEDIFF(d, 0, StartDate),0)
ORDER BY [Customer Creation Date]

Note: [Customer Creation Date] and [Number Of New Customers] column alias just provide more readable column headers.

3. Finally, how can you format the DATETIME to only show the DATE part (after all the TIME part is now always midnight)?

The built in CONVERT function allows you to convert the DATETIME to a CHAR array using a specific format. The format is a bit arbitrary and needs looking up, but 101 is the U.S. standard mm/dd/yyyy, and 103 is the U.K. standard dd/mm/yyyy.

SELECT CONVERT(CHAR(10), DATEADD(d, DATEDIFF(d, 0, StartDate),0), 103) [Customer Creation Date], COUNT(*) [Number Of New Customers]
FROM Customer
WHERE StartDate >= '2011-01-01'
GROUP BY DATEADD(d, DATEDIFF(d, 0, StartDate),0)
ORDER BY [Customer Creation Date]

 #

 


Sunday, February 20, 2011 #

When I found out yesterday that one of my top 5 development tools .NET Reflector will no longer be free at the end of February, I thought I'd see if work had started on a good open source alternative...and guess what...work on ILSpy is already well underway!!

There seems to be a difference of opinion on what Red Gate said when they purchased .NET Reflector from Lutz Roeder in 2008. They say that they would try to keep it free, where as others think they promised to keep it free. Either way at the time I thought it was a smart purchase by Red Gate as it would raise their profile overnight within the .Net community.

But not only are they going to charge $35 for v7 (which is up to them), they have also time-bombed v6 to force users to pay. This I think will lower their profile overnight within the .Net community!!

Maybe they are been slightly naive in thinking the community wouldn't just write an alternative?

 #