2010-11-11 120 views
1

我已经编写了用于在ftp中创建新站点的代码。但它给了com例外。该代码已附上供参考。以编程方式创建ftp站点

异常细节

System.Runtime.InteropServices.COMException was unhandled 
    Message="Filename: \r\nError: Unrecognized element 'ftpServer'\r\n\r\n" 
    Source="" 
    ErrorCode=-2147023483 
    StackTrace: 
     at Microsoft.Web.Administration.Interop.IAppHostElement.GetElementByName(String bstrSubName) 
     at Microsoft.Web.Administration.ConfigurationElement.GetChildElement(String elementName) 
     at WindowsFormsTest.Form2.CreateFTPSite(String serverName, String siteName, String siteID) in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Form2.cs:line 48 
     at WindowsFormsTest.Form2.button1_Click(Object sender, EventArgs e) in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Form2.cs:line 25 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at WindowsFormsTest.Program.Main() in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

代码来创建FTP站点:

using(ServerManager serverManager = new ServerManager()) { 
     Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration(); 

     ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); 

     ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); 

     ConfigurationElement siteElement = sitesCollection.CreateElement("site"); 
     siteElement["name"] = @"eMentorftp"; 

     ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); 

     ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); 
     bindingElement["protocol"] = @"ftp"; 
     bindingElement["bindingInformation"] = @"*:21:"; 
     bindingsCollection.Add(bindingElement); 

     ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); 

     ConfigurationElement securityElement = ftpServerElement.GetChildElement("security"); 

     ConfigurationElement sslElement = securityElement.GetChildElement("ssl"); 
     sslElement["serverCertHash"] = @"53FC3C74A1978C734751AB7A14A3E48F70A58A84"; 
     sslElement["controlChannelPolicy"] = @"SslRequire"; 
     sslElement["dataChannelPolicy"] = @"SslRequire"; 

     ConfigurationElement authenticationElement = securityElement.GetChildElement("authentication"); 

     ConfigurationElement basicAuthenticationElement = authenticationElement.GetChildElement("basicAuthentication"); 
     basicAuthenticationElement["enabled"] = true; 

     ConfigurationElementCollection siteCollection = siteElement.GetCollection(); 

     ConfigurationElement applicationElement = siteCollection.CreateElement("application"); 
     applicationElement["path"] = @"/"; 

     ConfigurationElementCollection applicationCollection = applicationElement.GetCollection(); 

     ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory"); 
     virtualDirectoryElement["path"] = @"/"; 
     virtualDirectoryElement["physicalPath"] = @"D:\Ftp"; 
     applicationCollection.Add(virtualDirectoryElement); 
     siteCollection.Add(applicationElement); 
     sitesCollection.Add(siteElement); 

     serverManager.CommitChanges(); 

回答

5

你可以告诉你的错误是异常的。看看在异常堆栈跟踪的第一行:

所有的

System.Runtime.InteropServices.COMException was unhandled Message="Filename: \r\nError: Unrecognized element 'ftpServer'\r\n\r\n" Source="" ErrorCode=-2147023483

首先,我们看到“无法识别的元素‘的FTPServer’” - 这应该告诉我们很多关于发生了什么事,当我们看你的代码。但除此之外,我们还提供了一个错误代码,告诉我们为什么抛出COMException。如果我们错误代码解码成一个unsigned int(十六进制),我们得到:

0x80070585 

这相当于一个错误代码:

  • 8:失败
  • 7:Win32的
  • 585:(1413十进制):无效指数(ERROR_INVALID_INDEX)

有关错误代码的详细信息,请参阅HRESULTWin32 error codes

因此,我们抛出了一个COMException,因为我们使用了一个无效的索引。在异常消息中我们得到一个“无法识别的元素”ftpServer'“。

Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration(); 
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); 
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); 
ConfigurationElement siteElement = sitesCollection.CreateElement("site"); 

然后,您的网站中创建一个名称,创建该网站的绑定集绑定:看你的代码,你通过告诉sitesCollection添加名为“地盘”的新元素创建一个新的ConfigurationElement siteElement:

siteElement["name"] = @"eMentorftp"; 
ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); 
ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); 
bindingElement["protocol"] = @"ftp"; 
bindingElement["bindingInformation"] = @"*:21:"; 
bindingsCollection.Add(bindingElement); 

不幸的是,你再尝试使用名称 “的FTPServer” 获得该网站的一个子元素:

ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); 

但是,在ConfigurationEle siteElement - 刚刚创建 - 没有名为ftpServer的子项!所以我们得到一个无效的索引并抛出异常。你是否想在这行代码中创建一个子元素?另外,我想象它是一个COMException,因为.NET接口与底层IIS7 COM对象互操作。不是Microsoft.Web命名空间的专家(根本就不是),这只是一个猜测。

+1

+1帮助解码错误代码。 – 2011-06-28 22:11:23