// 用 appcmd 方式获得 所有虚拟目录的信息 (不仅仅是 dp2OPAC 虚拟目录) public static int GetAllVirtualInfoByAppCmd(out List<OpacAppInfo> infos, out string strError) { strError = ""; infos = new List<OpacAppInfo>(); // appcmd.exe 路径 string fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv/appcmd.exe"); if (File.Exists(fileName) == false) { strError = "IIS appcmd 尚未安装"; return -1; } List<string> lines = new List<string>(); // list apps // list config /section:Sites lines.Add("list config /section:Sites"); // parameters: // lines 若干行参数。每行执行一次 // return: // -1 出错 // 0 成功。strError 里面有运行输出的信息 int nRet = InstallHelper.RunCmd( fileName, lines, false, out strError); if (nRet == -1) return -1; XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strError); } catch (Exception ex) { strError = "Sites 描述 '" + strError + "' 装入 XMLDOM 时出错: " + ex.Message; return -1; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("//application"); foreach (XmlElement application in nodes) { OpacAppInfo info = new OpacAppInfo(application); infos.Add(info); } return infos.Count; }
// 用 appcmd 方式获得 dp2OPAC 实例信息 public static int GetOpacInfoByAppCmd(out List<OpacAppInfo> infos, out string strError) { strError = ""; infos = new List<OpacAppInfo>(); // appcmd.exe 路径 string fileName = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv/appcmd.exe"); if (File.Exists(fileName) == false) { strError = "IIS appcmd 尚未安装"; return -1; } List<string> lines = new List<string>(); // list apps // list config /section:Sites lines.Add("list config /section:Sites"); // parameters: // lines 若干行参数。每行执行一次 // return: // -1 出错 // 0 成功。strError 里面有运行输出的信息 int nRet = InstallHelper.RunCmd( fileName, lines, false, out strError); if (nRet == -1) return -1; #if NO // List<string> results = new List<string>(); StringBuilder result = new StringBuilder(); string strErrorInfo = ""; try { int i = 0; foreach (string arguments in lines) { ProcessStartInfo info = new ProcessStartInfo() { FileName = fileName, Arguments = arguments, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true, }; using (Process process = Process.Start(info)) { process.OutputDataReceived += new DataReceivedEventHandler((s, e1) => { // results.Add(e1.Data); if (string.IsNullOrEmpty(e1.Data) == false) result.Append(e1.Data + "\r\n"); } ); process.ErrorDataReceived += new DataReceivedEventHandler((s, e1) => { strErrorInfo = e1.Data; } ); process.BeginOutputReadLine(); while (true) { Application.DoEvents(); if (process.WaitForExit(500) == true) break; } } for (int j = 0; j < 10; j++) { Application.DoEvents(); Thread.Sleep(1); } i++; } } finally { } if (string.IsNullOrEmpty(strErrorInfo) == false) { strError = strErrorInfo; return -1; } #endif /* C:\Windows\System32\inetsrv>appcmd list apps APP "Default Web Site/" (applicationPool:DefaultAppPool) APP "Default Web Site/dp2OPAC" (applicationPool:DefaultAppPool) * * */ /* C:\Windows\System32\inetsrv>appcmd list config /section:Sites <system.applicationHost> <sites> <siteDefaults> <bindings> </bindings> <limits /> <logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles"> <customFields> </customFields> </logFile> <traceFailedRequestsLogging directory="%SystemDrive%\inetpub\logs\FailedRe qLogFiles" /> <ftpServer> <connections /> <security> <dataChannelSecurity /> <commandFiltering> </commandFiltering> <ssl /> <sslClientCertificates /> <authentication> <anonymousAuthentication /> <basicAuthentication /> <clientCertAuthentication /> <customAuthentication> <providers> </providers> </customAuthentication> </authentication> <customAuthorization> <provider /> </customAuthorization> </security> <customFeatures> <providers> </providers> </customFeatures> <messages /> <fileHandling /> <firewallSupport /> <userIsolation> <activeDirectory /> </userIsolation> <directoryBrowse /> <logFile /> </ftpServer> </siteDefaults> <applicationDefaults applicationPool="DefaultAppPool" /> <virtualDirectoryDefaults allowSubDirConfig="true" /> <site name="Default Web Site" id="1"> <bindings> <binding protocol="http" bindingInformation="*:80:" /> </bindings> <limits /> <logFile> <customFields> </customFields> </logFile> <traceFailedRequestsLogging /> <applicationDefaults /> <virtualDirectoryDefaults /> <ftpServer> <connections /> <security> <dataChannelSecurity /> <commandFiltering> </commandFiltering> <ssl /> <sslClientCertificates /> <authentication> <anonymousAuthentication /> <basicAuthentication /> <clientCertAuthentication /> <customAuthentication> <providers> </providers> </customAuthentication> </authentication> <customAuthorization> <provider /> </customAuthorization> </security> <customFeatures> <providers> </providers> </customFeatures> <messages /> <fileHandling /> <firewallSupport /> <userIsolation> <activeDirectory /> </userIsolation> <directoryBrowse /> <logFile /> </ftpServer> <application path="/"> <virtualDirectoryDefaults /> <virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" /> </application> <application path="/dp2OPAC"> <virtualDirectoryDefaults /> <virtualDirectory path="/" physicalPath="c:\test" /> </application> </site> </sites> </system.applicationHost> * * */ XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strError); } catch (Exception ex) { strError = "Sites 描述 '"+strError+"' 装入 XMLDOM 时出错: " +ex.Message; return -1; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("//application"); foreach (XmlElement application in nodes) { OpacAppInfo info = new OpacAppInfo(application); if (string.IsNullOrEmpty(info.PhysicalPath) == false // 如果不是应用目录,则 PhysicalPath 可能为空 && info.IsOPAC() == true) infos.Add(info); } return infos.Count; }
// 查找 dp2OPAC 路径 // return: // -1 出错 // 其他 返回找到的路径个数 int FindOpacPath(out List<OpacAppInfo> infos, out string strError) { strError = ""; infos = new List<OpacAppInfo>(); for (int i = 0; ; i++) { string strSitePath = "IIS://localhost/W3SVC/" + (i + 1); try { if (DirectoryEntry.Exists(strSitePath) == false) break; } catch (COMException ex) { if ((uint)ex.ErrorCode == 0x80005000) { strError = "IIS 尚未启用"; return -1; } } string strOpacPath = strSitePath + "/ROOT/dp2OPAC"; if (DirectoryEntry.Exists(strOpacPath) == false) continue; DirectoryEntry entry = new DirectoryEntry(strOpacPath); OpacAppInfo info = new OpacAppInfo(entry); if (string.IsNullOrEmpty(info.PhysicalPath) == false) // 如果不是应用目录,则 PhysicalPath 可能为空 infos.Add(info); } return infos.Count; }
// 查找 dp2OPAC 路径 // return: // -1 出错 // 其他 返回找到的路径个数 public static int GetOpacInfo(out List<OpacAppInfo> infos, out string strError) { strError = ""; infos = new List<OpacAppInfo>(); Version version = OpacAppInfo.GetIisVersion(); if (version.Major >= 7) { // 使用 appcmd 获得信息 // appcmd list apps return GetOpacInfoByAppCmd(out infos, out strError); } // 用 Active Directory 方法 for (int i = 0; ; i++) { string strSitePath = "IIS://localhost/W3SVC/" + (i + 1); try { if (DirectoryEntry.Exists(strSitePath) == false) break; } catch (COMException ex) { if ((uint)ex.ErrorCode == 0x80005000) { strError = "IIS 尚未启用"; return -1; } } string strRootPath = strSitePath + "/ROOT"; if (DirectoryEntry.Exists(strRootPath) == false) continue; DirectoryEntry entry = new DirectoryEntry(strRootPath); foreach (DirectoryEntry child in entry.Children) { if (child.SchemaClassName != "IIsWebVirtualDir") continue; OpacAppInfo info = new OpacAppInfo(child); if (string.IsNullOrEmpty(info.PhysicalPath) == false // 如果不是应用目录,则 PhysicalPath 可能为空 && info.IsOPAC() == true) infos.Add(info); } } return infos.Count; }
// 查找 dp2OPAC 路径 // return: // -1 出错 // 其他 返回找到的路径个数 public static int GetOpacInfo(out List <OpacAppInfo> infos, out string strError) { strError = ""; infos = new List <OpacAppInfo>(); Version version = OpacAppInfo.GetIisVersion(); if (version.Major >= 7) { // 使用 appcmd 获得信息 // appcmd list apps return(GetOpacInfoByAppCmd(out infos, out strError)); } // 用 Active Directory 方法 for (int i = 0; ; i++) { string strSitePath = "IIS://localhost/W3SVC/" + (i + 1); try { if (DirectoryEntry.Exists(strSitePath) == false) { break; } } catch (COMException ex) { if ((uint)ex.ErrorCode == 0x80005000) { strError = "IIS 尚未启用"; return(-1); } } string strRootPath = strSitePath + "/ROOT"; if (DirectoryEntry.Exists(strRootPath) == false) { continue; } DirectoryEntry entry = new DirectoryEntry(strRootPath); foreach (DirectoryEntry child in entry.Children) { if (child.SchemaClassName != "IIsWebVirtualDir") { continue; } OpacAppInfo info = new OpacAppInfo(child); if (string.IsNullOrEmpty(info.PhysicalPath) == false && // 如果不是应用目录,则 PhysicalPath 可能为空 info.IsOPAC() == true) { infos.Add(info); } } } return(infos.Count); }
void SetDefaultVirtualDirValue() { string strError = ""; List <OpacAppInfo> infos = null; // 用 appcmd 方式获得 所有虚拟目录的信息 (不仅仅是 dp2OPAC 虚拟目录) int nRet = OpacAppInfo.GetAllVirtualInfoByAppCmd(out infos, out strError); if (nRet == -1) { goto ERROR1; } for (int i = 0; ; i++) { string strVirtualDir = "/dp2OPAC"; if (i > 0) { strVirtualDir = "/dp2OPAC" + (i + 1).ToString(); } // 已经存在的虚拟目录不能使用 // 查找一个虚拟目录是否存在 // return: // -1 不存在 // 其他 数组元素的下标 nRet = OpacAppInfo.Find(infos, this.comboBox_site.Text, strVirtualDir); if (nRet != -1) { continue; } // 注意检查,不能是别的instance的数据目录 if (this.VerifyDataDir != null) { VerifyEventArgs e1 = new VerifyEventArgs(); e1.Value = strVirtualDir; this.VerifyDataDir(this, e1); if (String.IsNullOrEmpty(e1.ErrorInfo) == false) { continue; } } if (this.VerifyInstanceName != null) { VerifyEventArgs e1 = new VerifyEventArgs(); e1.Value = this.comboBox_site.Text + strVirtualDir; this.VerifyInstanceName(this, e1); if (String.IsNullOrEmpty(e1.ErrorInfo) == false) { continue; } } this.textBox_instanceName.Text = strVirtualDir; return; } return; ERROR1: MessageBox.Show(this, strError); }