我正在使用WMI(System.Managment)和C#开发ASP.Net网站供应软件。
我正在尝试在我位于局域网某处的“目标服务器”上创建FTP站点,同时在我的主机上执行代码。创建后我无法启动FTP站点。
在以下行中报告的错误是“无效的类”:
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”对象在"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或更高版本上运行正常。
我做错了什么但无法猜到哪里。请帮助。感谢。
由于
答案 0 :(得分:1)
答案 1 :(得分:0)
我自己想到了这个问题,因为我在错误的班级上调用了这个函数。必须在服务器类上调用“Start”方法。即IIsWebServer.Start()或IIsFtpServer.Start()