Monday, February 18, 2013

Function to Split String in Chunks of Given Max Size

Friends,

Following is the function to split a given string in chunks of given maximum size:

 public static List SplitString(string stringToSplit, int maxLength)
        {
            List lstStr = new List();

            if (!String.IsNullOrEmpty(stringToSplit) && maxLength > 0 && stringToSplit.Length - maxLength >= 0)
            {
                int count = 0;
                for (int i = 0; i < stringToSplit.Length; )
                {
                    count = stringToSplit.Length - i >= maxLength ? maxLength : stringToSplit.Length - i;
                    lstStr.Add(stringToSplit.Substring(i, count));
                    i = i + maxLength;
                }
            }

            return lstStr;
        }

Tuesday, February 12, 2013

Creating a website in IIS7 programatically

This post will let you know about how to create a website in the IIS7 panel programatically.
For this purpose you need to add the reference to the Microsoft.Web.Administration found in the following paths depending on your system configuration:

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
OR 
C:\Windows\SysWOW64\inetsrv\Microsoft.Web.Administration.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Web.Administration;

namespace WebSiteCreator
{
    ///


    /// Interaction logic for MainWindow.xaml
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CreateWebSite()
        {
            string siteName = txtSiteName.Text.Trim();
            string applicationPoolName = txtAppPoolName.Text.Trim();
            string virtualDirectoryPath = txtVDPath.Text.Trim();
            string virtualDirectoryPhysicalPath = txtVDPhysicalPath.Text.Trim();
            string ipAddress = txtIpAddress.Text.Trim();
            string tcpPort = txtPort.Text.Trim();
            string hostHeader = txtHostHeader.Text.Trim();
            string applicationPath = txtAppPath.Text.Trim();
            long siteId = 1;

            using (ServerManager servMgr = new ServerManager())
            {
                Site site = servMgr.Sites[siteName];

                if (site != null)
                    return;

                ApplicationPool appPool = servMgr.ApplicationPools[applicationPoolName];

                if (appPool == null)
                {
                    appPool = servMgr.ApplicationPools.CreateElement();
                    appPool.SetAttributeValue("name", applicationPoolName);
                }

                foreach (Site mysite in servMgr.Sites)
                {
                    if (mysite.Id > siteId)

                        siteId = mysite.Id;
                }

                siteId++;

                site = servMgr.Sites.CreateElement();

                site.SetAttributeValue("name", siteName);

                site.Id = siteId;

                site.Bindings.Clear();

                string bindingSetting = ipAddress + ":" + tcpPort + ":" + hostHeader;

                Microsoft.Web.Administration.Binding binding = site.Bindings.CreateElement();

                binding.Protocol = "http";

                binding.BindingInformation = bindingSetting;

                site.Bindings.Add(binding);

                Microsoft.Web.Administration.Application app = site.Applications.CreateElement();

                app.Path = applicationPath;

                app.ApplicationPoolName = applicationPoolName;

                Microsoft.Web.Administration.VirtualDirectory vdir = app.VirtualDirectories.CreateElement();

                vdir.Path = virtualDirectoryPath;

                vdir.PhysicalPath = virtualDirectoryPhysicalPath;

                app.VirtualDirectories.Add(vdir);

                site.Applications.Add(app);

                servMgr.Sites.Add(site);

                servMgr.CommitChanges();
            }
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            CreateWebSite();
        }
    }
}

Link to download the software - Download

Thursday, February 7, 2013

The specified service has been marked for deletion

When you get this message then chances are that you're trying to uninstall a windows service.
Generally, this error happens when you don't stop the service before attempting to uninstall it or/and some associated process or handle hangs around, preventing the service from being uninstalled.

One common reason for this happening is services.msc - you have to take care of it particularly as chances of getting this error are also when you have stopped the service before running your uninstaller, and still have the services.msc window open.

The name of the service will still be shown in the service listing if your services.msc window is open. So before trying to restart the system, make sure you close this window in your session and any other active rdp session.

If you close and reopen the services.msc window, you'll probably find that your service has now been successfully deleted.