asp.net iis create a virtual directory to create a virtual directory

These days I received a small file management system project inquiry, stepped pit.

In fact, all came to a simple function, generally require customers to have a lot of pdf documents, in order to facilitate the management, the equivalent of all to develop a document management system, I just have an existing document management system, you can modify the next. Pdf which customer needs into other letter, and the application can not be together, this solution is very convenient. After the on-line system, because customers are sub-directories in pdf, if hundreds of virtual directory is created manually, then, is a bit cumbersome. Requiring code. Recorded as follows:

     /// <summary>
        // create a virtual directory
        /// </summary>
        /// <param name = "hostName"> host name or IP address of the site visit 127.0.0.1 </ param>
        /// <param name = "vDirName"> need to create a virtual directory name </ param>
        /// <param name = "mappingPath"> Address Mapping </ param>
        /// <param name = "websiteID"> web application ID </ param>
        public static void CreateVDir(string websiteID, string vDirName, string mappingPath, string hostName = "127.0.0.1")
        {
            /*
             In the creation of error will be reported: System.UnauthorizedAccessException: Access is denied (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). 
        Online search because the question of powers Solution is: default in IIS application pools -> Advanced ...--> tags into Local System can solve the problem */ // iis whether there is a need to create a virtual directory, true: there is false: there is no But there hasvdirn = false; try { // IIS://127.0.0.1/W3SVC/10/ROOT string path = string.Format("IIS://{0}/W3SVC/{1}/ROOT", hostName, websiteID); DirectoryEntry rootfolder = new DirectoryEntry(path); // Loop through, we need to create a virtual directory to determine whether there iis website above foreach (System.DirectoryServices.DirectoryEntry v in rootfolder.Children) { if (v.Name == vDirName) { hasVdirName = true; break; } } // If there is no need to create if (hasVdirName) return; // to the collection DirectoryEntry newVirDir = rootfolder.Children.Add(vDirName, rootfolder.SchemaClassName); // specify the actual virtual directory mapping newVirDir.Properties["Path"][0] = mappingPath; // commit changes newVirDir.CommitChanges(); // commit changes rootfolder.CommitChanges(); newVirDir.Close(); rootfolder.Close(); rootfolder.Dispose (); newVirDir.Dispose (); } catch (Exception ex) { // error log processing } }

Guess you like

Origin www.cnblogs.com/changsen-wang/p/11267025.html