static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ApplicationExit += new EventHandler(Application_ApplicationExit); try { PopupForm popupForm = new PopupForm("Initializing settings for the first time. This may take some time"); popupForm.Show(); InstallPorts(); popupForm.Close(); //stop COM intermediate service for the appliation if it is running. ServiceController service = new ServiceController("ComIntermediateService"); if (service.Status == ServiceControllerStatus.Running) { service.Stop(); } } catch (Exception ex) { Log.Input(ex); } Application.Run(new MainForm()); }
private void btnFinish_Click(object sender, EventArgs e) { PopupForm popupForm = null; try { btnFinish.Enabled = false; popupForm = new PopupForm("Resetting virtual ports. please wait."); if (ValidateChildren(ValidationConstraints.Enabled)) { if (!_saveVCOMDetails) { MessageBox.Show(this, "Please fix errors before continue"); return; } } popupForm.Show(this); COMDeviceInfo deviceInfo = new COMDeviceInfo(); deviceInfo.DevicePortName = DeviceSerialPort.Port; VCOMInfo vCOMInfo = new VCOMInfo(); if (txtDeviceName1.Text.Trim().Length > 0) { vCOMInfo.DeviceName = txtDeviceName1.Text; vCOMInfo.IPAddress = txtIPAddress1.Text; vCOMInfo.VirtualPortName = drpVCOM1.SelectedItem.ToString(); deviceInfo.VirtualDevices.Add(vCOMInfo); } if (txtDeviceName2.Text.Trim().Length > 0) { vCOMInfo = new VCOMInfo(); vCOMInfo.DeviceName = txtDeviceName2.Text; vCOMInfo.IPAddress = txtIPAddress2.Text; vCOMInfo.VirtualPortName = drpVCOM2.SelectedItem.ToString(); deviceInfo.VirtualDevices.Add(vCOMInfo); } if (txtDeviceName3.Text.Trim().Length > 0) { vCOMInfo = new VCOMInfo(); vCOMInfo.DeviceName = txtDeviceName3.Text; vCOMInfo.IPAddress = txtIPAddress3.Text; vCOMInfo.VirtualPortName = drpVCOM3.SelectedItem.ToString(); deviceInfo.VirtualDevices.Add(vCOMInfo); } if (txtDeviceName4.Text.Trim().Length > 0) { vCOMInfo = new VCOMInfo(); vCOMInfo.DeviceName = txtDeviceName4.Text; vCOMInfo.IPAddress = txtIPAddress4.Text; vCOMInfo.VirtualPortName = drpVCOM4.SelectedItem.ToString(); deviceInfo.VirtualDevices.Add(vCOMInfo); } if (txtDeviceName5.Text.Trim().Length > 0) { vCOMInfo = new VCOMInfo(); vCOMInfo.DeviceName = txtDeviceName5.Text; vCOMInfo.IPAddress = txtIPAddress5.Text; vCOMInfo.VirtualPortName = drpVCOM5.SelectedItem.ToString(); deviceInfo.VirtualDevices.Add(vCOMInfo); } if (deviceInfo.VirtualDevices.Count > 0) { //log COM config data into JSON file File.WriteAllText(_configFileName, JsonConvert.SerializeObject(deviceInfo)); string filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); if (File.Exists(filePath + "\\setupc.exe")) { try { int portIndex = 0; foreach (VCOMInfo vCOM in deviceInfo.VirtualDevices) { int i = drpVCOM1.Items.IndexOf(vCOM.VirtualPortName); System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = filePath + "\\setupc.exe"; startInfo.Arguments = "remove " + i; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); portIndex++; process = new System.Diagnostics.Process(); startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = filePath + "\\setupc.exe"; startInfo.Arguments = "Install PortName=" + vCOM.VirtualPortName + " PortName=" + vCOM.VirtualPortName.Replace("A", "B"); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); } } catch (Exception ex) { Log.Input(ex); MessageBox.Show(this, "Failed to recreate virtual ports"); return; } } popupForm.Close(); MessageBox.Show(this, "Device setup is completed"); Application.Exit(); } else { MessageBox.Show(this, "No devices available"); } btnFinish.Enabled = true; } catch (Exception ex) { if (popupForm != null) { popupForm.Close(); } Log.Input(ex); MessageBox.Show(this, ex.Message); } finally { btnFinish.Enabled = true; } }