【发布时间】:2010-02-13 12:12:41
【问题描述】:
我正在使用 WMI(System.Managment) 和 C# 开发 ASP.Net 网站配置软件。
我正在尝试在位于 LAN 某处的“目标服务器”上创建 FTP 站点,同时在我的主机上执行代码。创建 FTP 站点后,我无法启动它。
报告的错误是 "Invalid Class" 在以下行:
ManagementObject site = new ManagementObject(
new ManagementPath(string.Format(@"IIsFtpServer.Name='MSFTPSVC/{0}'", siteId)), null);
**site.InvokeMethod("Start", null);**
这是我的完整功能。
public static String CreateFtpsite(String serverName,
String ip,String ServerComment,int AccessFlags,
String pathToRoot, String hostName, String domainName, int port)
{
//ConnectionOptions options = new ConnectionOptions();
//options.Authentication = AuthenticationLevel.Connect;
//options.EnablePrivileges = true;
//options.Impersonation = ImpersonationLevel.Impersonate;
ConnectionOptions options = SetUpAuthorization();
ManagementScope scope =
new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISv2", serverName), options);
scope.Connect();
ManagementObject oW3SVC = new ManagementObject(scope,
new ManagementPath(@"IIsFtpService='MSFTPSVC'"), null);
ManagementBaseObject[] serverBindings = new ManagementBaseObject[3];
/*serverBindings[0] = CreateServerBinding(scope,
string.Format("{0}.{1}", hostName, domainName), ip, port);
*/
serverBindings[0] = CreateServerBinding(scope,
string.Format("{0}", hostName, domainName), ip, port);
serverBindings[1] = CreateServerBinding(scope,
string.Format(ip, hostName, domainName), ip, port);
serverBindings[2] = CreateServerBinding(scope,
string.Format("127.0.0.1", hostName, domainName), ip, port);
ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
inputParameters["ServerBindings"] = serverBindings;
inputParameters["ServerComment"] = ServerComment;
inputParameters["PathOfRootVirtualDir"] = pathToRoot;
ManagementBaseObject outParameter =
oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
string siteId = Convert.ToString(
outParameter.Properties["ReturnValue"].Value).Replace(
"IIsFtpServer='MSFTPSVC/", "").Replace("'", "");
ManagementObject oFtpVirtDir = new ManagementObject(scope,
new ManagementPath(string.Format(
@"IIsFtpVirtualDirSetting.Name='MSFTPSVC/{0}/root'", siteId)), null);
oFtpVirtDir.Properties["AccessFlags"].Value = AccessFlags ;
oFtpVirtDir.Properties["Path"].Value = pathToRoot;
ManagementObject oFtpVirtDirProperties = new ManagementObject(scope,
new ManagementPath(string.Format(@"IIsFtpServerSetting.Name='MSFTPSVC/{0}'", siteId)), null);
oFtpVirtDirProperties.Properties["AllowAnonymous"].Value = true;
oFtpVirtDirProperties.Properties["AnonymousOnly"].Value = true;
oFtpVirtDirProperties.Properties["AnonymousUserName"].Value = @"DevIIS\Administrator";
oFtpVirtDirProperties.Properties["AnonymousUserPass"].Value = "Passw0rd";
oFtpVirtDirProperties.Properties["MaxConnections"].Value = 555;
oFtpVirtDirProperties.Properties["ServerAutoStart"].Value = true;
oFtpVirtDirProperties.Properties["UserIsolationMode"].Value = 1;
oFtpVirtDirProperties.Properties["ConnectionTimeout"].Value = 1234 ;
oFtpVirtDirProperties.Properties["LogFileTruncateSize"].Value = 54321;
oFtpVirtDirProperties.Put();
ManagementObject site = new ManagementObject(
new ManagementPath(string.Format(@"IIsFtpServer.Name='MSFTPSVC/{0}'", siteId)), null);
site.InvokeMethod("Start", null); //Error occurs Here (Invalid Class)
return siteId;
}
public static ConnectionOptions SetUpAuthorization()
{
ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
options.Username = @"DevIIS\Administrator";
options.Password = "Passw0rd";
return options;
}
在分析代码时,我发现“站点”对象在"site.ClassPath". 上抛出“无效类”异常
我也尝试使用以下行,但出现相同的错误。
ManagementObject site = new ManagementObject(scope,
new ManagementPath(Convert.ToString(
outParameter.Properties["ReturnValue"].Value)), null);
site.InvokeMethod("Start", null);
但是 FTP 站点已创建并设置了所有属性,但它不是通过代码启动的。可以通过转到 IIS 管理器来手动启动它。
代码在 IIS 6.0 上运行良好(因为我使用的是 WMI)我希望它在 IIS 7.0 或更高版本上也能正常运行。
我做错了,但不知道在哪里。请帮忙。谢谢。
谢谢
【问题讨论】:
标签: asp.net iis-7 iis-6 ftp wmi