Search code examples
c#iis-6virtual-directory

Application Name remain empty for virtual directory in IIS 6 using C#


I am creating virtual directory in IIS 6 using C#.

I am able to create the virtual directory but the 'Application Name' field remains empty.

alt text

Here's the code I am using

DirectoryEntry iisRoot = new DirectoryEntry("IIS://" + Environment.MachineName + "/W3SVC");

string webName = "1";

string virdir = "TestApp1";

string installpath = @"C:\MyWeb\Application\";

        try
        {
            string iisPath = string.Format("IIS://{0}/W3SVC/{1}/Root", Environment.MachineName, webName);
            Console.WriteLine(iisPath);
            iisRoot = new DirectoryEntry(iisPath);

            DirectoryEntry vdir = iisRoot.Children.Add(virdir, iisRoot.SchemaClassName);

            vdir.Properties["Path"][0] = installpath;
            vdir.Properties["AppFriendlyName"][0] = virdir;
            vdir.Properties["EnableDefaultDoc"][0] = true;
            vdir.Properties["DefaultDoc"][0] = "Login.aspx,default.htm,default.aspx,default.asp";
            vdir.Properties["AspEnableParentPaths"][0] = true;
            vdir.CommitChanges();
            vdir.Invoke("AppCreate", true);
        }
        catch (Exception e)
        {
            Console.Write(e.Message + "\n" + e.StackTrace);
        }

I have used 'AppFriendlyName' property but still it does not show up in virtual directory properties.


Solution

  • Finally I got the answer.

    The 'AppFriendlyName' property must nbe set after the vdir.CommitChanges();

    so the code should be

    DirectoryEntry iisRoot = new DirectoryEntry("IIS://" + Environment.MachineName + "/W3SVC");
    
    string webName = "1";
    
    string virdir = "TestApp1";
    
    string installpath = @"C:\MyWeb\Application\";
    
            try
            {
                string iisPath = string.Format("IIS://{0}/W3SVC/{1}/Root", Environment.MachineName, webName);
                Console.WriteLine(iisPath);
                iisRoot = new DirectoryEntry(iisPath);
    
                DirectoryEntry vdir = iisRoot.Children.Add(virdir, iisRoot.SchemaClassName);
    
                vdir.Properties["Path"][0] = installpath;
                vdir.Properties["EnableDefaultDoc"][0] = true;
                vdir.Properties["DefaultDoc"][0] = "Login.aspx,default.htm,default.aspx,default.asp";
                vdir.Properties["AspEnableParentPaths"][0] = true;
                vdir.CommitChanges();
                vdir.Invoke("AppCreate", true);
                vdir.Properties["AppFriendlyName"][0] = virdir;
                vdir.CommitChanges();
            }
            catch (Exception e)
            {
                Console.Write(e.Message + "\n" + e.StackTrace);
            }