如何用C#编写一个有实用型的window server(类似IIS)的那种
主 题: 如何用C#编写一个有实用型的window server(类似IIS)的那种。
作 者: qqq123 (qqq123)
等 级: ^^^
信 誉 值: 100
所属论坛: .NET技术 C#
问题点数: 100
回复次数: 8
发表时间: 2003-11-27 10:12:07
我学过Socket,也编写过一些window server 的例子代码,可是这些都没有实用价值,我想问C#是否可以编写出一个实用型的window server?
如果可以的话,在编写过程中应当注意什么? 你有什么想法和建议 或 相关资料 请 不吝赐教 !!! 重分感谢!!!
回复人: hlj321(hlj321) ( 三级(初级)) 信誉:100 2003-11-27 10:20:13 得分:0
昨天在网上下载了一个对IIS控制的一个类,如需要可给你。
回复人: NicholasZhr(只在乎一时的灵感) ( 二级(初级)) 信誉:100 2003-11-27 10:30:51 得分:0
偶也想要!谢谢楼上的了!
mailto:NicholasZhr@163.com
回复人: polarlm(polarlm) ( 三级(初级)) 信誉:100 2003-11-27 10:31:30 得分:100
using System;
using System.DirectoryServices;
using System.Collections;
namespace WayControls.IIS
{
/// <summary>
/// IISWebServer的状态
/// </summary>
public enum IISServerState
{
/// <summary>
///
/// </summary>
Starting = 1,
/// <summary>
///
/// </summary>
Started = 2,
/// <summary>
///
/// </summary>
Stopping = 3,
/// <summary>
///
/// </summary>
Stopped = 4,
/// <summary>
///
/// </summary>
Pausing = 5,
/// <summary>
///
/// </summary>
Paused = 6,
/// <summary>
///
/// </summary>
Continuing = 7
}
/// <summary>
/// IISManagement 的摘要说明。
/// </summary>
public class IISManagement
{
/// <summary>
///
/// </summary>
public IISWebServerCollection WebServers = new IISWebServerCollection();
internal static string Machinename = "localhost";
/// <summary>
///
/// </summary>
public IISManagement()
{
start();
}
/// <summary>
///
/// </summary>
/// <param name="MachineName">机器名,默认值为localhost</param>
public IISManagement(string MachineName)
{
if (MachineName.ToString() != "")
Machinename = MachineName;
start();
}
private void start()
{
DirectoryEntry Service = new DirectoryEntry("IIS://" + Machinename + "/W3SVC");
DirectoryEntry Server;
DirectoryEntry Root = null;
DirectoryEntry VirDir;
IEnumerator ie = Service.Children.GetEnumerator();
IEnumerator ieRoot;
IISWebServer item;
IISWebVirtualDir item_virdir;
bool finded = false;
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
item = new IISWebServer();
item.index = Convert.ToInt16(Server.Name);
item.ServerComment = (string)Server.Properties["ServerComment"][0];
item.AccessRead = (bool)Server.Properties["AccessRead"][0];
item.AccessScript = (bool)Server.Properties["AccessScript"][0];
item.DefaultDoc = (string)Server.Properties["DefaultDoc"][0];
item.EnableDefaultDoc = (bool)Server.Properties["EnableDefaultDoc"][0];
item.EnableDirBrowsing = (bool)Server.Properties["EnableDirBrowsing"][0];
ieRoot = Server.Children.GetEnumerator();
while (ieRoot.MoveNext())
{
Root = (DirectoryEntry)ieRoot.Current;
if (Root.SchemaClassName == "IIsWebVirtualDir")
{
finded = true;
break;
}
}
if (finded)
{
item.Path = Root.Properties["path"][0].ToString();
}
item.Port = Convert.ToInt16(((string)Server.Properties["Serverbindings"][0]).Substring(1, ((string)Server.Properties["Serverbindings"][0]).Length - 2));
this.WebServers.Add_(item);
ieRoot = Root.Children.GetEnumerator();
while (ieRoot.MoveNext())
{
VirDir = (DirectoryEntry)ieRoot.Current;
if (VirDir.SchemaClassName != "IIsWebVirtualDir" && VirDir.SchemaClassName != "IIsWebDirectory")
continue;
item_virdir = new IISWebVirtualDir(item.ServerComment);
item_virdir.Name = VirDir.Name;
item_virdir.AccessRead = (bool)VirDir.Properties["AccessRead"][0];
item_virdir.AccessScript = (bool)VirDir.Properties["AccessScript"][0];
item_virdir.DefaultDoc = (string)VirDir.Properties["DefaultDoc"][0];
item_virdir.EnableDefaultDoc = (bool)VirDir.Properties["EnableDefaultDoc"][0];
if (VirDir.SchemaClassName == "IIsWebVirtualDir")
{
item_virdir.Path = (string)VirDir.Properties["Path"][0];
}
else if (VirDir.SchemaClassName == "IIsWebDirectory")
{
item_virdir.Path = Root.Properties["Path"][0] + "\\" + VirDir.Name;
}
item.WebVirtualDirs.Add_(item_virdir);
}
}
}
}
回复人: polarlm(polarlm) ( 三级(初级)) 信誉:100 2003-11-27 10:32:02 得分:0
/// <summary>
/// 创建站点
/// </summary>
/// <param name="iisServer"></param>
public static void CreateIISWebServer(IISWebServer iisServer)
{
if (iisServer.ServerComment.ToString() == "")
throw (new Exception("IISWebServer的ServerComment不能为空!"));
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server;
int i = 0;
IEnumerator ie = Service.Children.GetEnumerator();
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
if (Convert.ToInt16(Server.Name) > i)
i = Convert.ToInt16(Server.Name);
// if( Server.Properties["Serverbindings"][0].ToString() == ":" + iisServer.Port + ":" )
// {
// Server.Invoke("stop",new object[0]);
// }
}
}
i++;
try
{
iisServer.index = i;
Server = Service.Children.Add(i.ToString(), "IIsWebServer");
Server.Properties["ServerComment"][0] = iisServer.ServerComment;
Server.Properties["Serverbindings"].Add(":" + iisServer.Port + ":");
Server.Properties["AccessScript"][0] = iisServer.AccessScript;
Server.Properties["AccessRead"][0] = iisServer.AccessRead;
Server.Properties["EnableDirBrowsing"][0] = iisServer.EnableDirBrowsing;
Server.Properties["DefaultDoc"][0] = iisServer.DefaultDoc;
Server.Properties["EnableDefaultDoc"][0] = iisServer.EnableDefaultDoc;
DirectoryEntry root = Server.Children.Add("Root", "IIsWebVirtualDir");
root.Properties["path"][0] = iisServer.Path;
Service.CommitChanges();
Server.CommitChanges();
root.CommitChanges();
//Server.Invoke("start",new object[0]);
}
catch (Exception es)
{
throw (es);
}
}
/// <summary>
/// 删除IISWebServer
/// </summary>
public static void RemoveIISWebServer(string ServerComment)
{
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server;
IEnumerator ie = Service.Children.GetEnumerator();
ServerComment = ServerComment.ToLower();
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
if (Server.Properties["ServerComment"][0].ToString().ToLower() == ServerComment)
{
Service.Children.Remove(Server);
Service.CommitChanges();
return;
}
}
}
}
/// <summary>
/// 删除IISWebServer
/// </summary>
public static void RemoveIISWebServer(int index)
{
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
try
{
DirectoryEntry Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
if (Server != null)
{
Service.Children.Remove(Server);
Service.CommitChanges();
}
else
{
throw (new Exception("找不到此IISWebServer"));
}
}
catch
{
throw (new Exception("找不到此IISWebServer"));
}
}
/// <summary>
/// 检查是否存在IISWebServer
/// </summary>
/// <param name="ServerComment">网站说明</param>
/// <returns></returns>
public static bool ExistsIISWebServer(string ServerComment)
{
ServerComment = ServerComment.Trim();
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server = null;
IEnumerator ie = Service.Children.GetEnumerator();
string comment;
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
comment = Server.Properties["ServerComment"][0].ToString().ToLower().Trim();
if (comment == ServerComment.ToLower())
{
return true;
}
}
}
return false;
}
/// <summary>
/// 返回指定的IISWebServer
/// </summary>
/// <param name="ServerComment"></param>
/// <returns></returns>
internal static DirectoryEntry returnIISWebserver(string ServerComment)
{
ServerComment = ServerComment.Trim();
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server = null;
IEnumerator ie = Service.Children.GetEnumerator();
string comment;
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
comment = Server.Properties["ServerComment"][0].ToString().ToLower().Trim();
if (comment == ServerComment.ToLower())
{
return Server;
}
}
}
return null;
}
/// <summary>
/// 返回指定的IISWebServer
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
internal static DirectoryEntry returnIISWebserver(int index)
{
DirectoryEntry Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
try
{
IEnumerator ie = Server.Children.GetEnumerator();
return Server;
}
catch
{
return null;
}
}
/// <summary>
/// 修改与给定的IISWebServer具有相同网站说明的站点配置
/// </summary>
/// <param name="iisServer">给定的IISWebServer</param>
public static void EditIISWebServer(IISWebServer iisServer)
{
if (iisServer.index == -1)
throw (new Exception("找不到给定的站点!"));
DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
DirectoryEntry Server;
IEnumerator ie = Service.Children.GetEnumerator();
while (ie.MoveNext())
{
Server = (DirectoryEntry)ie.Current;
if (Server.SchemaClassName == "IIsWebServer")
{
if (Server.Properties["Serverbindings"][0].ToString() == ":" + iisServer.Port + ":")
{
Server.Invoke("stop", new object[0]);
}
}
}
Server = returnIISWebserver(iisServer.index);
if (Server == null)
{
throw (new Exception("找不到给定的站点!"));
}
try
{
Server.Properties["ServerComment"][0] = iisServer.ServerComment;
Server.Properties["Serverbindings"][0] = ":" + iisServer.Port + ":";
Server.Properties["AccessScript"][0] = iisServer.AccessScript;
Server.Properties["AccessRead"][0] = iisServer.AccessRead;
Server.Properties["EnableDirBrowsing"][0] = iisServer.EnableDirBrowsing;
Server.Properties["DefaultDoc"][0] = iisServer.DefaultDoc;
Server.Properties["EnableDefaultDoc"][0] = iisServer.EnableDefaultDoc;
ie = Server.Children.GetEnumerator();
ie.MoveNext();
DirectoryEntry root = (DirectoryEntry)ie.Current;
root.Properties["path"][0] = iisServer.Path;
Server.CommitChanges();
root.CommitChanges();
Server.Invoke("start", new object[0]);
}
catch (Exception es)
{
throw (es);
}
}
回复人: chenwei518(makelive) ( 二级(初级)) 信誉:100 2003-11-27 10:36:45 得分:0
去看一看TCP/IP
回复人: hlj321(hlj321) ( 三级(初级)) 信誉:100 2003-11-27 10:43:11 得分:0
NicholasZhr : polarlm(polarlm) 发的就是
回复人: qqq123(qqq123) ( 三级(初级)) 信誉:100 2003-11-27 10:45:29 得分:0
Mail: shaleone2000@yahoo.com.cn
谢谢!
回复人: liduke(天下有雪) ( 五级(中级)) 信誉:100 2003-11-27 11:30:54 得分:0
我觉得还是多往remoting上着眼比较好
该问题已经结贴 ,得分记录: polarlm (100)、