c# 获取iis地址
2016-01-14 15:42
148 查看
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZQNB.Tool.Upgrade.Utilities
{
public class IISHelper
{
/// <summary>
/// 获取指定站名的路径
/// todo 应该做成用户可以选择的
/// </summary>
/// <returns></returns>
public static string GetSitePath()
{
var rootEntry = new DirectoryEntry("IIS://localhost/w3svc");
foreach (DirectoryEntry entry in rootEntry.Children)
{
if (entry.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Name: {0}", entry.Name);
Console.WriteLine("Path: {0}", IISWorker.GetWebsitePhysicalPath(entry));
Console.WriteLine("ServerBindings: {0}", entry.Properties["ServerBindings"].Value);
var serverComment = entry.Properties["ServerComment"].Value.ToString();
Console.WriteLine("ServerComment: {0}", serverComment);
if (serverComment == Config.SiteName)
{
return IISWorker.GetWebsitePhysicalPath(entry);
}
//DirectoryEntry virEntry = new DirectoryEntry(entry.Path + "/ROOT");
//foreach (DirectoryEntry entryVirtual in virEntry.Children)
//{
// if (entryVirtual.SchemaClassName.Equals("IIsWebVirtualDir", StringComparison.OrdinalIgnoreCase))
// {
// Console.WriteLine("SchemaClassName: {0}", entryVirtual.SchemaClassName);
// Console.WriteLine("Name: {0}", entryVirtual.Name);
// Console.WriteLine("Path: {0}", entryVirtual.Properties["Path"].Value);
// Console.WriteLine();
// }
//}
}
}
return null;
}
}
public class IISWorker
{
/// <summary>
/// 得到网站的物理路径
/// </summary>
/// <param name="rootEntry">网站节点</param>
/// <returns></returns>
public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
{
string physicalPath = "";
foreach (DirectoryEntry childEntry in rootEntry.Children)
{
if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
{
if (childEntry.Properties["Path"].Value != null)
{
physicalPath = childEntry.Properties["Path"].Value.ToString();
}
else
{
physicalPath = "";
}
}
}
return physicalPath;
}
}
}
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZQNB.Tool.Upgrade.Utilities
{
public class IISHelper
{
/// <summary>
/// 获取指定站名的路径
/// todo 应该做成用户可以选择的
/// </summary>
/// <returns></returns>
public static string GetSitePath()
{
var rootEntry = new DirectoryEntry("IIS://localhost/w3svc");
foreach (DirectoryEntry entry in rootEntry.Children)
{
if (entry.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Name: {0}", entry.Name);
Console.WriteLine("Path: {0}", IISWorker.GetWebsitePhysicalPath(entry));
Console.WriteLine("ServerBindings: {0}", entry.Properties["ServerBindings"].Value);
var serverComment = entry.Properties["ServerComment"].Value.ToString();
Console.WriteLine("ServerComment: {0}", serverComment);
if (serverComment == Config.SiteName)
{
return IISWorker.GetWebsitePhysicalPath(entry);
}
//DirectoryEntry virEntry = new DirectoryEntry(entry.Path + "/ROOT");
//foreach (DirectoryEntry entryVirtual in virEntry.Children)
//{
// if (entryVirtual.SchemaClassName.Equals("IIsWebVirtualDir", StringComparison.OrdinalIgnoreCase))
// {
// Console.WriteLine("SchemaClassName: {0}", entryVirtual.SchemaClassName);
// Console.WriteLine("Name: {0}", entryVirtual.Name);
// Console.WriteLine("Path: {0}", entryVirtual.Properties["Path"].Value);
// Console.WriteLine();
// }
//}
}
}
return null;
}
}
public class IISWorker
{
/// <summary>
/// 得到网站的物理路径
/// </summary>
/// <param name="rootEntry">网站节点</param>
/// <returns></returns>
public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
{
string physicalPath = "";
foreach (DirectoryEntry childEntry in rootEntry.Children)
{
if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
{
if (childEntry.Properties["Path"].Value != null)
{
physicalPath = childEntry.Properties["Path"].Value.ToString();
}
else
{
physicalPath = "";
}
}
}
return physicalPath;
}
}
}
相关文章推荐
- C#高级编程 反射 代码示例
- C# 新委托使用方法 Lambda表达式,运算符 =>
- c# 替换所有中文、标点符号,全角转半角
- 跟siki老师学C#第一天
- C#实现百度站长工具链接主动提交功能
- 关于C#下面windows服务读取注册表的问题
- C# String与string的区别
- C#中null和""的区别
- 转载: 黄聪:C#中 Excel列字母与数字的转换
- c#按照一定条件打印
- c#开发之多国语言解决方案gnu.gettext + poedit
- C#实现的18位身份证格式验证算法
- c#图片添加水印
- C# serialport控件的一些理解
- C#的历史及IDE总结
- C# Best Practices - Creating Good Methods
- C#调用dll函数时出错:视图加载格式不正常的程序
- C# 雕虫小技
- C#源代码—三角形面积、圆的面积
- C#源代码—姓名 请输入老师的编号、姓名、职称和部门