Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create a folder in virtual directory which is hosted in iis by using c sharp? I am using this in my project for creating a folder for every user...
Posted

Hi,

I am not sure where I got the origin of this code, but it was helpful for me.

C#
 private DirectoryEntry _iisServer;
        public IISAdmin(string serverName)
        {
            _serverName = serverName;
        }
        public IISAdmin()
        {
            _serverName = "localhost";
        }
        ~IISAdmin()
        {
            _iisServer.Dispose();
            _iisServer = null;
        }
public void Connect(string username,string password)
        {
            try
            {
                if (string.IsNullOrEmpty(username))
                    _iisServer = new DirectoryEntry("IIS://" + _serverName + "/w3svc");
                else
                    _iisServer = new DirectoryEntry("IIS://" + _serverName + "/w3svc", username, password);
            }
            catch (Exception e)
            {
                throw new Exception("Could not connect to: " + _serverName, e);
            }
        }
  public List<keyvaluepair><string,>> GetAvailableWebSites()
        {
            List<keyvaluepair><string,>> retVal = new List<keyvaluepair><string,>>();
            foreach (DirectoryEntry o in _iisServer.Children)
            {
                if (o.SchemaClassName == WebServerSchemaName)
                {
                    if (o.Properties["ServerComment"] != null && o.Properties["ServerComment"].Count > 0)
                        retVal.Add(new KeyValuePair<string,>(o.Name, (string)o.Properties["ServerComment"][0]));
                    else
                        retVal.Add(new KeyValuePair<string,>(o.Name, "[website]"));
                }
            }
            return retVal;
        }
  public void CreateVirtualDirectory(int WebHostId, string nameDirectory, string realPath)
        {
            DirectoryEntry WebsiteEntity = null;
            DirectoryEntry folderRoot = null;
            foreach (DirectoryEntry o in _iisServer.Children)
                if (o.Name == WebHostId.ToString())
                {
                    WebsiteEntity = o;
                    folderRoot = o.Children.Find("Root", VirDirSchemaName);
                    break;
                }
            if (folderRoot == null) throw new Exception("Website couldnot found!");
            try
            {
                DirectoryEntry newVirDir = folderRoot.Children.Add(nameDirectory, VirDirSchemaName);
                // Create a Application
                newVirDir.Invoke("AppCreate", true);
                newVirDir.Properties["AppFriendlyName"].Insert(0, nameDirectory);
                // Set Properties
                newVirDir.Properties["AccessRead"].Insert(0, true);
                newVirDir.Properties["Path"].Insert(0, realPath);
                // Save Changes
                newVirDir.CommitChanges();
                folderRoot.CommitChanges();
                WebsiteEntity.CommitChanges();
                _iisServer.CommitChanges();
            }
            catch (Exception e)
            {
                throw new Exception("Virtual Directory " + nameDirectory + " Already Exists", e);
            }
        }
</keyvaluepair></keyvaluepair></keyvaluepair>


Hope it can help you.
 
Share this answer
 
Comments
Patha Ashish 28-Mar-12 7:51am    
this was good for me
For IIS 7 and above it's better to use AppCmd.exe. See http://learn.iis.net/page.aspx/114/getting-started-with-appcmdexe/[^]
Else you have to install IIS 6 Compatibility components.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900