Sat
Jul 1
2006

Custom Configuration Section (.Net 2.0)

To add a custom configuration section to a project:

  • Add a reference to System.Configuration.dll
  • Add to your app.config:
    <configuration>
      <configSections>
        <section name="myConfiguration"
           type="MyProject.MyConfiguration, MyProject" />
      </configSections>
      <myConfiguration
        emailTo="MyEmailAddress@domain.com"
      />
    </configuration>
  • Add the class MyConfiguration.cs:

    using System;
    using System.Configuration;
     
    namespace MyProject
    {
        class MyConfiguration : ConfigurationSection
        {
            public static MyConfiguration Current
            {
                get { return ConfigurationManager.GetSection("myConfiguration") 
                                  as MyConfiguration; }
            }
     
            [ConfigurationProperty("emailTo")]
            public string EmailTo
            {
                get { return (string)this["emailTo"]; }
                set { this["emailTo"] = value; }
            }
     
        }
    }
  • To use in code:
    Trace.WriteLine(MyConfiguration.Current.EmailTo);
Fri
Jun 30
2006

Quick XML Serialization

public void SaveXml(string filename)
{
    XmlSerializer xml = new XmlSerializer(this.GetType());
    using (FileStream fs = new FileStream(filename, FileMode.Create))
    {
        xml.Serialize(fs, this);
    }
}
 
public static Accounts LoadXml(string filename)
{
    XmlSerializer xml = new XmlSerializer(typeof(Accounts));
    FileInfo f = new FileInfo(filename);
    if (!f.Exists || f.Length == 0)
        return new Accounts();
    using (FileStream fs = new FileStream(filename, FileMode.Open))
    {
        return (Accounts)xml.Deserialize(fs);
    }
}
Sun
Jun 25
2006

Syntax Highlighting in WordPress

Syntax highlighting with iG:Syntax Hiliter is mostly working now. This is the best excuse for hosting my own WordPress installation. I’ve found a few tweaks are needed to get this going:

  • I am not using a WYSIWYG editor. This is the biggest problem so far. The built-in rich editor collapses whitespace in my code so you lose indentation. I have been experimenting with other editors.
  • Updated the .syntax_hilite style in syntax_hilite_css.css with text-align:left since my current theme uses justified text. Also removed the fixed width statements.
  • Tweaked the C# color scheme to match Visual Studio.
Sun
Jun 25
2006

Redirect Trace Messages to a TextBox

///
/// Outputs trace messages to a textbox. To use call
/// Trace.Listeners.Add(new TextBoxTraceListener(textBox1).
/// All subsequent calls to Trace.WriteLine() are copied
/// to textBox1.
///
public class TextBoxTraceListener : TraceListener
{
    private TextBox textBox;
 
    ///
    /// Constructor
    ///
    public TextBoxTraceListener(TextBox p_textBox)
    {
        textBox = p_textBox;
    }
 
    ///
    /// Write a message to the textbox
    ///
    public override void Write(string message)
    {
        textBox.Text += message.Replace("\n", "\r\n");
    }
 
    ///
    /// Write a message with newline to the textbox
    ///
    ///<param name="message" />
    public override void WriteLine(string message) 
    { 
        this.Write(message + "\n"); 
    } 
}
Sun
Jun 25
2006

Setting up WordPress on WinXP

Getting WordPress running on WinXP was surprisingly easy. Here are my notes on the install.

  • WinXP SP2
  • PHP 5.1
  • MySQL 5.0
  • WordPress 2.0

Installing PHP on IIS

  • Download and install the PHP Windows installer package. I used all the default options.
  • Grant IUSR_ write access to c:\php\sessiondata and c:\php\uploadtemp
  • Added index.php as a default document in IIS (IIS Admin -> Default Web Site -> Properties -> Documents tab -> Add… -> index.php
  • To test the install create a file named c:\inetpub\wwwroot\test.php with the contents
    [php]
    phpinfo();
    ?>
    [/php]and then use your browser to view the page at http://localhost/test.php
  • Open c:\windows\php.ini
    Uncomment the line extension=php_mysql.dll
    Change the extension_dir line to read: extension_dir = "./ext/"
    Save the file.
  • Download the PHP binaries (not the windows installer version).
    Extract libmysql.dll and ext/pgp_mysql.dll into c:\php and c:\php\ext

Installing MySql

  • Download and install MySQL. I used the complete version but in hindsight I probably only needed the essentials.
  • Download and install MySQL Administrator from the same site.

Create a WordPress database

  • In the Catalogs section, create a new schema named wordpress
  • In the User Administration section, create a user named wp_user, enter a password.
  • Right-click on the new user, Add Host From Which The User Can Connect, enter localhost.
  • Select localhost which should now appear under the new user on the left.
  • On the right-hand side, select the Schema Privileges tab, select the wordpress database.
  • Click the << button to assign this user full access to the wordpress database. Warning: Be sure to select the localhost node on the left when assigning privileges so the user only has permissions when connecting locally.

Setup WordPress

  • Download WordPress.
  • Extract to c:\inetpub\wwwroot\wordpress
  • In the new wordpress folder, rename wp-config-sample.php to wp-config.php.
  • Edit the file and change the database name, user name and password.
  • Open your browser to http://localhost/wordpress/wp-admin/install.php
  • Download themes and extensions.
  • I had to peform this fix before I was able to upload files.

Enable Permalinks in WordPress with IIS

  • Create a file wp-404-handler.php with the code
    [php]
    $my_wp_url = "http://" . $_SERVER['SERVER_NAME'] . "";
    $_SERVER['REQUEST_URI'] = substr($_SERVER['QUERY_STRING'], strpos($_SERVER['QUERY_STRING'], $my_wp_url)+strlen($my_wp_url));
    $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
    include('index.php');
    ?>
    [/php]
    This code is from WordPress Permalinks on IIS. I have changed the code slightly (line 2) since I host my Wordpress installation in the root directory.

  • Configure IIS to redirect 404 errors to “/” (no quotes)
  • UrlScan, must be configured to allow periods in the URL. Open c:\windows\system32\inetsrv\urlscan\urlscan.ini. Change option to read AllowDotInPath=1
  • Turn on permalinks in the WordPress permalinks options
  • Restart IIS

Some other good articles on setting up WordPress, PHP, MySQL:
David Seah’s WIMP Notes
WordPress 5-minute install
Web Thang Installing PHP for IIS on Win XP
WordPress Permalinks on IIS
Easy Permalinks in IIS

« Newer Posts

© 2009 Brian Low. All rights reserved.