I try to get an external IP address using the NATUPNPLib library, but a COMException exception is COMException .

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NATUPNPLib; using System.Net; namespace ExternalIP { public partial class Form1 : Form { private UPnPNAT objRouter; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show(GetExternalIPFromRouter()); } public string GetExternalIPFromRouter() { objRouter = new UPnPNAT(); IStaticPortMappingCollection objPortMapCollection = objRouter.StaticPortMappingCollection; if ((objPortMapCollection == null) == false) { string strExternalIP = null; foreach (IStaticPortMapping temp in objPortMapCollection)//здесь вылетает исключение { strExternalIP = temp.ExternalIPAddress.ToString(); break; } return strExternalIP; } else { return "Sorry! Unable to Get External/WAN IP!"; } } } } 

Two questions:

  1. Why the exception takes off?
  2. In some cases, the exception does not crash, but instead of IP, the string "Sorry! Unable to Get External / WAN IP!" Is returned. It is treated by turning off the power adapter. Why is the StaticPortMappingCollection empty and how to fix it?

Exception text:

 System.Runtime.InteropServices.COMException не обработано HResult=-2147220984 Message=Компонент, предоставляемый пользователем, или произошло исключение для клиента (Исключение из HRESULT: 0x80040208) Source=mscorlib ErrorCode=-2147220984 StackTrace: в System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) в System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) в System.Runtime.InteropServices.CustomMarshalers.EnumeratorViewOfEnumVariant.MoveNext() в ExternalIP.Form1.GetExternalIPFromRouter() в c:\Users\Artem\Documents\Visual Studio 2012\Projects\ExternalIP\ExternalIP\Form1.cs:строка 35 в ExternalIP.Form1.button1_Click(Object sender, EventArgs e) в c:\Users\Artem\Documents\Visual Studio 2012\Projects\ExternalIP\ExternalIP\Form1.cs:строка 25 в System.Windows.Forms.Control.OnClick(EventArgs e) в System.Windows.Forms.Button.OnClick(EventArgs e) в System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) в System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) в System.Windows.Forms.Control.WndProc(Message& m) в System.Windows.Forms.ButtonBase.WndProc(Message& m) в System.Windows.Forms.Button.WndProc(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) в System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) в System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) в System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) в System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) в System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) в System.Windows.Forms.Application.Run(Form mainForm) в ExternalIP.Program.Main() в c:\Users\Artem\Documents\Visual Studio 2012\Projects\ExternalIP\ExternalIP\Program.cs:строка 19 в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() в System.Threading.ThreadHelper.ThreadStart_Context(Object state) в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) в System.Threading.ThreadHelper.ThreadStart() InnerException: 
  • Attach an exception to the question - gil9red
  • @ gil9red attached - Art7

0