Example #1
0
 private void ServerEnum()
 {
     try
     {
         if (this.textBox_OpcServerIp.Text == string.Empty)
         {
             MessageBox.Show("请输入IP地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             return;
         }
         if (this.textBox_OpcServerIp.Text != "localhost" && !RegexMatcher.IsIpAddressValidated(this.textBox_OpcServerIp.Text))//用正则表达式验证IP地址
         {
             MessageBox.Show("请输入正确格式的IP地址!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             return;
         }
         this.comboBox_OpcServerList.Items.Clear();//清空已显示的OPC Server列表
         this.IpAddress = this.textBox_OpcServerIp.Text;
         if (this.KepServer == null)
         {
             this.KepServer = new OPCServer();
         }
         //object serverList = this.KepServer.GetOPCServers(IpAddress);
         //Array array = (Array)serverList;
         Array array = (Array)(object)this.KepServer.GetOPCServers(IpAddress);
         //假如Server列表为空,退出方法,否则为ListBoxControl添加Item
         if (array.Length == 0)
         {
             return;
         }
         //for (int i = 1; i <= array.Length; i++)
         //    this.comboBox_OpcServerList.Items.Add((string)array.GetValue(i));
         this.comboBox_OpcServerList.Items.AddRange(array.Cast <string>().ToArray());
         //if (this.comboBox_OpcServerList.Items.Count > 0)
         this.comboBox_OpcServerList.SelectedIndex = 0;
     }
     //假如获取OPC Server过程中引发COMException,即代表无法连接此IP的OPC Server
     catch (Exception ex)
     {
         MessageBox.Show("无法连接此IP地址的OPC Server!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }