Human Readable File Size

7th April 2009

This function will allow you to convert a system data size (in bytes) to a more user friendly and understandable format. It will generate the best format in either GB, MB, KB or Bytes.

private string HumanReadableSize(long _size)
{
	if (_size >= 1024 * 1024 * 1024)
	{
		return string.Format("{0:########0.##} GB", (_size) / (1024 * 1024 * 1024));
	}
 	else if (_size >= 1024 * 1024)
	{
		return string.Format("{0:####0.##} MB", (_size) / (1024 * 1024));
	}
	else if (_size >= 1024)
	{
		return string.Format("{0:####0.##} KB", (_size) / 1024);
	}
 	else
	{
		return string.Format("{0} bytes", _size);
	}
}

A Simple C# ComboItem Class

2nd April 2009

If you need to add a list of key/value pairs to a combo box you can use this neat little class to do so. Simply add the new class to the ComboBox's item collection.

public class ComboBoxItem
{
    public string Key;
    public string Value;

    public ComboBoxItem(string Key, string Value)
    {
        this.Key = Key;
        this.Value = Value;
    }

    public string GetKey()
    {
        return this.Key;
    }

    public override string ToString()
    {
        return this.Value;
    }
}


// add to combo box
ComboBox1.Items.Add(new ComboBoxItem("Key", "Value"));

Uploading a File Over FTP using VB.net

2nd April 2009

This routine uses FtpWebRequest to upload a file to a remote server.

Public Sub UploadFile(ByVal RemoteFile As String, ByVal LocalFile As String, ByVal User As String, ByVal Pass As String)
    Dim clsRequest As FtpWebRequest = CType(WebRequest.Create("ftp://" & RemoteFile), FtpWebRequest)
    clsRequest.Credentials = New NetworkCredential(User, Pass)
    clsRequest.UseBinary = True
    clsRequest.Method = WebRequestMethods.Ftp.UploadFile

    Dim bFile() As Byte = My.Computer.FileSystem.ReadAllBytes(LocalFile)

    Using clsStream As IO.Stream = clsRequest.GetRequestStream()
        clsStream.Write(bFile, 0, bFile.Length)
        clsStream.Close()
    End Using

    MsgBox("File uploaded")
End Sub

Checking for Valid Emails in PHP

16th March 2009

If you need to check that an email address is valid within PHP one of the easiest ways is to use regular expressions. Theis simple functions checks that an email address contains only valid characters and has an @ and period in the correct places.

function IsEmail($email)
{
    $pattern = '^([A-Za-z0-9\.|-|_]{1,60})([@])([A-Za-z0-9\.|-|_]{1,60})(\.)([A-Za-z]{2,3})$';
    return ereg($pattern, $email);	
}

Enabling XHTML Conformance in ASP.NET 2.0

2nd March 2009

By default ASP.NET does not render XHTML compatible code - specifically for form elements and elements with self closing tags. It therefore does not validate correctly when checking against any html validator. You can force ASP.NET to generate XHTML code by adding an extra tag to your 'web.config' file (within the 'system.web' section).

<system.web>
   <!-- other elements here -->
   <xhtmlConformance mode="Strict" />
</system.web>

Making a C# Function Thread-Safe

10th February 2009

You can make a subroutine thread-safe by checking the InvokeRequired property of the current control. If this returns true then the calling thread is different to the GUI thread and the routine must be called using a delegate.

private delegate void ThreadSafeHandler();

private void ThreadSafe()
{
  if (this.InvokeRequired)
  {
    this.Invoke(new ThreadSafeHandler(ThreadSafe));
  }
  else
  {
    // general stuff in here
    // gui calls will be thread safe
  }
}

Office 2003 on Vista 64bit

23rd January 2009

After securing a new development laptop at work I opted to start using Vista Buisness 64-bit, and in doing become the unofficial 64-bit tester for the rest of the office. As normal after installing a fresh copy of Windows I started installing our standard set of office and development software - Visual Studio, Apache, PHP, Netbeans...

I managed to successfully install virtually all the applications I required except for 1 - Office 2003. I have used Microsoft's Office 2003 for a while now without ever having any major problems but for some reason it didn't want to play ball and the install just wouldn't complete. A few moments into the beginning of the install I kept getting:

“Error 1913. Setup cannot update file C:\Windows\SysWOW64\mapisvc.inf. Verify that the file exists in your system and that you have sufficient permissions to update it.”

After giving up with the install several times I eventually came across I solution that seemed to do the trick. It turns out that its simply all down to permissions on the mapisvc.inf file as detailed in this useful post.