首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式创建ftp站点

以编程方式创建ftp站点
EN

Stack Overflow用户
提问于 2010-11-11 06:43:21
回答 1查看 2.2K关注 0票数 1

我已经编写了在ftp中创建新站点的代码。但它给了com例外。本代码附后以供参考。

异常详细信息包括

代码语言:javascript
复制
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站点的代码:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2011-01-14 20:30:09

您可以从异常中看出您的错误是什么。查看异常堆栈跟踪中的第一行:

System.Runtime.InteropServices.COMException未处理Message=“文件名:\r\n错误:未识别元素‘ftpServer’\r\n\n”“Source="”ErrorCode=-2147023483“

首先,我们看到“未被识别的元素'ftpserver'”--这将告诉我们在查看代码时发生了什么。但除此之外,我们还得到了一个错误代码,它可以准确地告诉我们抛出COMException的原因。如果我们将错误代码解码为无符号int (十六进制),就会得到:

代码语言:javascript
复制
0x80070585

这对应于以下错误代码:

  • 8:失败
  • 7: Win32
  • 585:(1413小数点):无效索引(ERROR_INVALID_INDEX)

有关错误代码的更多信息,请参见赫鲁特Win32错误码

因此,我们抛出了一个COMException,因为我们使用了一个无效的索引。我们在异常消息中得到一个“未被识别的元素'ftpServer'”。查看您的代码,通过告诉ConfigurationElement siteElement添加一个名为“site”的新元素来创建一个新的sitesCollection:

代码语言:javascript
复制
Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
ConfigurationElement siteElement = sitesCollection.CreateElement("site");

然后为站点创建一个名称,并在站点的绑定集合中创建绑定:

代码语言:javascript
复制
siteElement["name"] = @"eMentorftp";
ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings");
ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
bindingElement["protocol"] = @"ftp";
bindingElement["bindingInformation"] = @"*:21:";
bindingsCollection.Add(bindingElement);

不幸的是,您随后尝试使用名称“ftpServer”获取站点的子元素:

代码语言:javascript
复制
ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); 

但是,刚刚制作的ConfigurationElement siteElement却没有一个名叫ftpServer的孩子!因此,我们得到一个无效的索引并抛出一个异常。您是否打算在这一行代码中创建一个子元素?

顺便说一句,我认为这是一个COMException,因为.NET接口与底层IIS7 COM对象互操作。不是Microsoft.Web名称空间的专家(一点也不),这只是猜测。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4151977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档