gpt4 book ai didi

c# - 以编程方式创建 ftp 站点

转载 作者:太空宇宙 更新时间:2023-11-03 20:38:21 26 4
gpt4 key购买 nike

我已经编写了在 ftp 中创建新站点的代码。但它给 com 异常(exception)。附上代码供引用。

异常详细信息是

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();

最佳答案

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

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

首先,我们看到“Unrecognized element 'ftpserver'”——当我们查看您的代码时,它应该告诉我们很多关于发生的事情。但除此之外,我们还提供了一个错误代码,它告诉我们抛出 COMException 的确切原因。如果我们将错误代码解码为无符号整数(十六进制),我们会得到:

0x80070585

这对应于错误代码:

  • 8:失败
  • 7: Win32
  • 585:(十进制 1413):无效索引 (ERROR_INVALID_INDEX)

有关错误代码的更多信息,请参阅 HRESULTWin32 error codes .

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

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

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

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"); 

但是,刚刚创建的 ConfigurationElement siteElement 没有名为 ftpServer 的子项!所以我们得到一个无效的索引并抛出异常。您是要在这行代码中创建一个子元素吗?

顺便说一句,我想这是一个 COMException,因为 .NET 接口(interface)与底层 IIS7 COM 对象的互操作。不是 Microsoft.Web 命名空间方面的专家(根本不是),这只是一个猜测。

关于c# - 以编程方式创建 ftp 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4151977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com