asp.net Create Virtual Directory iis Create Virtual Directory

Keywords: ASP.NET IIS

These days I received a small project of file management query system and trampled through the pit.

In fact, the functions are very simple, roughly requiring customers to have many pdf documents, in order to facilitate management, all to develop a file management system equivalent to, I happen to have a ready-made file management system, can be modified. Among them, customers require pdf to be placed in other disc characters, not with the application, which is very convenient to solve. After the system is online, because the customer's pdf is distributed by directory, if hundreds of virtual directories are created manually, it will be a bit cumbersome. So code implementation is needed. The records are as follows:

     /// <summary>
        /// Create virtual directories
        /// </summary>
        /// <param name="hostName">host name or website access IP address 127.0.0.1</param>
        /// <param name="vDirName">Name of virtual directory to be created </param>
        /// <param name="mappingPath">mapping address</param>
        /// <param name="website ID">website application ID</param>
        public static void CreateVDir(string websiteID, string vDirName, string mappingPath, string hostName = "127.0.0.1")
        {
            /*
             Errors are reported when created: System. Unauthorized Access Exception: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED) 
        Online search is due to permission issues The solution is to change the default application pool in IIS - > Advanced Settings - > Identity to Local System. */ // Is there a virtual directory that needs to be created in iis, true: false: non-existent var hasVdirName = 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 to determine if the virtual directory you need to create exists on the iis website foreach (System.DirectoryServices.DirectoryEntry v in rootfolder.Children) { if (v.Name == vDirName) { hasVdirName = true; break; } } // If it exists, it does not need to be created if (hasVdirName) return; // Join set DirectoryEntry newVirDir = rootfolder.Children.Add(vDirName, rootfolder.SchemaClassName); // Specify the actual directory for virtual directory mapping newVirDir.Properties["Path"][0] = mappingPath; // Submit changes newVirDir.CommitChanges(); // Submit changes rootfolder.CommitChanges(); newVirDir.Close(); rootfolder.Close(); rootfolder.Dispose(); newVirDir.Dispose(); } catch (Exception ex) { // Error log processing } }

Posted by mrjameer on Tue, 08 Oct 2019 04:48:03 -0700