I’m in the middle of upgrading a web site to .Net 4.5 while reworking some architecture. I found myself wanting to use the same site name on my local IIS so that I can continue to maintain the old code until the migration is done, and hopefully there would be a minimal amount of work to switch over the rest of the developers on the team to the new project when I’m done. I had done some coding for IIS 7 a while ago, but the cobwebs were thick so I was struggling. It took a couple of hours of searching but I finally came across how to do this. 

I ended up making a small winform app that I can just click a button and switch between the two solutions as needed. I’ll probably end up sending this to the other developers on the team when we do the change over to to new code just to make it really easy. Firstly, a couple of textboxes to input the paths, and a couple of buttons on it to trigger the change. Next I added a couple of entries to the settings file to hold the default paths that will end up in the textboxes.

Generate the button click events, then head over to the code behind. Now, add a reference to Microsoft.Web.Administration, which lives in windir\system32\inetsrv, and then add a using statement for it in the code behind file.

        public Form1()
        {
            InitializeComponent();

            txt2010.Text = Properties.Settings.Default.Path2010;
            txt2012.Text = Properties.Settings.Default.Path2012;
        }

        private void btn2010_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Path2010 = txt2010.Text;            
            Properties.Settings.Default.Save();

            ServerManager oMan = new ServerManager();
            oMan.Sites["www.example.com"].Applications[0].VirtualDirectories[0].PhysicalPath = Properties.Settings.Default.Path2010;
            oMan.CommitChanges();
        }

        private void btn2012_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Path2012 = txt2012.Text;            
            Properties.Settings.Default.Save();

            ServerManager oMan = new ServerManager();
            oMan.Sites["www.example.com"].Applications[0].VirtualDirectories[0].PhysicalPath = Properties.Settings.Default.Path2012;
            oMan.CommitChanges();
        }

In the form load, I’m setting the default settings to the textboxes. In the clicks, I’m saving any changes to the setting, setting it to the correct site, then committing it. Really easy, once you find the right documentation!

Comments


Comments are closed