| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410 |
- using System;
- using System.Collections.Generic;
- using System.Configuration.Install;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.ServiceProcess;
- using System.Windows.Forms;
- using Curtain.DataAccess;
- using Curtain.Extension.ExObjectConvert;
- using Curtain.Extension.ExString;
- using Curtain.Helpers;
- using Curtain.Log;
- using Curtain.Net.Sockets.PLC.Model.Melsec;
- using Curtain.Net.Sockets.PLC.Model.Siemens;
- using Curtain.Systems.Cryptography;
- using Curtain.WCF.Proxy;
- using Curtain.Windows;
- using PLC_S;
- using PLC_S.Proxy;
- namespace PLC_WF
- {
- public partial class F_Main : Form
- {
- private PLC_S_S PLC_S_S = null;
- private readonly Logger logger = Logger.CreateLogger("WF");
- public F_Main()
- {
- InitializeComponent();
- logger.FileExistDays = 30;
- logger.FilePrefix = "";
- logger.FileSuffix = "WF";
- DataTable dataTable = new DataTable("PLC_S_T");
- dataTable.Columns.Add("code");
- dataTable.Columns.Add("name");
- dataTable.Columns.Add("description");
- //xuwei add 2023-07-24
- dataTable.Rows.Add("AA", "[AA]自动接口", "自动调用接口");
- //xuwei add 2024-01-02
- dataTable.Rows.Add("ETPS_EAA", "[EAA]自动轮询接口", "自动轮询调用接口");
- //xuwei add 2022-01-10
- dataTable.Rows.Add("GA", "[GA]自动", "自动识别模块");
- //xuwei add 2022-05-18
- dataTable.Rows.Add("EPTS_DA", "[DA]自动采集设备参数", "自动采集设备参数");
- // 新陶对接PLC温湿度数据
- dataTable.Rows.Add("EPTS_XT", "[XT]新陶", "3#新陶PLC温湿度数据");
- dataTable.Rows.Add("EPTS_XT_D", "[XT_D]新陶干燥房状态", "3#新陶4楼干燥房状态");
- // 5楼
- dataTable.Rows.Add("EPTS_HPGP", "[HPGP]高压注浆", "3#5F@高压成型");
- // 5楼
- dataTable.Rows.Add("EPTS_HPGP_FLAG", "[HPGP_FLAG]高压注浆线状态", "3#5F@高压成型注浆状态");
- dataTable.Rows.Add("GC_QR", "[GC_QR]产品型号【翻转用】", "3#5F@产品型号翻转用");
- // 5楼-4楼称重
- dataTable.Rows.Add("GW1", "[GW1]5F4F称重(1条码)", "3#5F4F@称重");
- // 对接乾润获取第二个条码
- dataTable.Rows.Add("GW2", "[GW2]5F4F称重(2条码)", "3#5F4F@称重");
- // 3楼
- dataTable.Rows.Add("GL_QR2", "[GL_QR2]3#刮登", "3#2F@刮登(乾润)");
- //防伪码验证 xuwei add 2021-12-30
- dataTable.Rows.Add("GL_QR2_SE", "[GL_QR2_SE]3#刮登(防伪码)", "3#2F@刮登(防伪码)(乾润)");
- dataTable.Rows.Add("GL_QR", "[GL_QR]毛坯库(入)", "3#3F@毛坯入库(乾润)");
- dataTable.Rows.Add("GP_QR1", "[GP_QR1]精坯库(入)前", "3#3F@精坯入库前(乾润)");
- dataTable.Rows.Add("GP_QR2", "[GP_QR2]精坯库(入)", "3#3F@精坯入库(乾润)");
- dataTable.Rows.Add("GP_JM3", "[GP_JM3]精坯库(出)", "3#3F@精坯出库(金马)");
- dataTable.Rows.Add("GC_DM", "[GC_DM]产品型号+条码【打磨前】", "3#3F@打磨前");
- dataTable.Rows.Add("EPTS_GP", "[GP]打磨计件", "3#3F@打磨计件");
- dataTable.Rows.Add("GLA_B", "[GLA_B]产品型号+重量+吹风计件【施釉前】", "3#3F@施釉前");
- dataTable.Rows.Add("GLA_A", "[GLA_A]产品重量+施釉房+施釉计件【施釉后】", "3#3F@施釉后");
- //防伪码绑定 xuwei add 2021-12-08
- dataTable.Rows.Add("GLA_AS", "[GLA_AS]产品重量+施釉房+施釉计件【施釉后防伪码】", "3#3F@施釉后防伪码");
- // 2楼
- dataTable.Rows.Add("GC_KL", "[GC_KL]产品型号+条码【登窑前】", "3#2F@登窑前");
- dataTable.Rows.Add("EPTS_KL", "[KL]登窑计件", "3#2F@登窑");
- dataTable.Rows.Add("EPTS_KUL", "[KUL]卸窑计件", "3#2F@卸窑");
- // 2楼 成检
- dataTable.Rows.Add("EPTS_GLK_A", "[GLK_A]轮巡测漏气", "3#2F@成检交接(一检)");
- dataTable.Rows.Add("GL_CF", "[GL_CF]产品等级【成检分线】", "3#2F@成检分线");
- dataTable.Rows.Add("GC", "[GC]产品型号【放水件、包盖子】", "3#1F@放水件、包盖子");
- // 1楼 成检
- dataTable.Rows.Add("GC_SS", "[GC_SS]产品型号+条码【试水前-测内漏】", "3#1F@试水前");
- dataTable.Rows.Add("EPTS_GLK_W", "[GLK_W]轮巡测内漏", "3#1F@成检(测内漏)");
- // 1楼 次品
- dataTable.Rows.Add("DA_IN", "[DA_IN]次品区(入)", "");
- dataTable.Rows.Add("EPTS_DA_OUT", "[DA_OUT]次品区(出)", "");
- // 1楼 包装
- //dataTable.Rows.Add("GC", "[GC]产品型号【放水件、包盖子】", "3#1F@包盖子");
- dataTable.Rows.Add("BP", "[BP]包装打印", "3#1F@包装打印");
- dataTable.Rows.Add("EPTS_PZ", "[PZ]包装码垛(产品型号、商标、高压标识)", "3#1F@包装码垛");
- dataTable.Rows.Add("EPTS_TK", "[TK]包装计件", "3#1F@包装计件");
- cboPLCST.DisplayMember = "name";
- cboPLCST.ValueMember = "code";
- this.txtPort.Tag = 1;
- cboPLCST.DataSource = dataTable;
- this.txtPort.Tag = null;
- GetSetting();
- this.btnGetDB_Click(null, null);
- this.btnGetMES_Click(null, null);
- this.btnLogLenR_Click(null, null);
- this.Text = $"{(PLC_S_S.DisplayName.IsNullOrWhiteSpace() ? PLC_S_S.ServiceName : PLC_S_S.DisplayName)}";
- }
- private void GetSetting()
- {
- PLC_S_S = PLC_S_S.Get();
- this.txtPort.Tag = 1;
- this.txtPort.Text = PLC_S_S.Port.ToString();
- if (!PLC_S_S.PLCServiceType.IsNullOrEmpty())
- {
- this.cboPLCST.SelectedValue = PLC_S_S.PLCServiceType;
- }
- this.txtPort.Tag = null;
- this.chkAutoStart.Checked = PLC_S_S.AutoStart;
- this.txtServiceName.Text = PLC_S_S.ServiceName;
- this.txtDisplayName.Text = PLC_S_S.DisplayName;
- this.txtDescription.Text = PLC_S_S.Description;
- this.txtInterval.Text = PLC_S_S.Interval + "";
- this.txtEPTS.Text = PLC_S_S.EPTS_CODE;
- }
- private void SetSetting()
- {
- //PLC_S_S PLC_S_S = PLC_S_S.Get();
- PLC_S_S.Port = this.txtPort.Text.ToInt32();
- PLC_S_S.PLCServiceType = this.cboPLCST.SelectedValue + "";
- PLC_S_S.AutoStart = this.chkAutoStart.Checked;
- PLC_S_S.ServiceName = this.txtServiceName.Text.Trim();
- PLC_S_S.DisplayName = this.txtDisplayName.Text.Trim();
- PLC_S_S.Description = this.txtDescription.Text.Trim();
- PLC_S_S.Interval = this.txtInterval.Text.Trim().ToInt32();
- PLC_S_S.EPTS_CODE = this.txtEPTS.Text.Trim();
- PLC_S_S.Set(PLC_S_S);
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(this.txtPort.Text))
- {
- MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtPort.Focus();
- return;
- }
- //if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
- //{
- // MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtServiceName.Focus();
- // return;
- //}
- //if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
- //{
- // MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtDisplayName.Focus();
- // return;
- //}
- SetSetting();
- //if (PLC_S_S.PLCServiceType == "BP1")
- //{
- // string[] s = textBox1.Lines;
- // PLC_S.ServerModel.BarcodePrint1 bb = new PLC_S.ServerModel.BarcodePrint1();
- // bb.bs = s;
- // bb.Start(PLC_S_S.Port);
- // PLC_Server.ServerSocket = bb;
- //}
- //else
- //{
- // PLC_Server.Start();
- //}
- PLC_Server.Start();
- SetESControl();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- if (_fff != null)
- {
- _fff.Text = $"[{PLC_Server.PLC_S_S.DisplayName}]";
- (PLC_Server.ServerSocket as IShowFormLog).FormLogShow = _fff;
- _fff.ShowFormLog = (PLC_Server.ServerSocket as IShowFormLog);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- try
- {
- PLC_Server.Stop();
- SetNSControl();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private F_Log _fff = null;
- private void btnLog_Click(object sender, EventArgs e)
- {
- try
- {
- if (PLC_Server.ServerSocket is IShowFormLog showFormLog)
- {
- if (showFormLog == null)
- {
- _fff = new F_Log();
- _fff.Show();
- return;
- }
- F_Log f = showFormLog.FormLogShow as F_Log;
- _fff = f;
- if (f == null)
- {
- f = new F_Log();
- f.Text = $"{f.Text }[{PLC_Server.PLC_S_S.DisplayName}]";
- showFormLog.FormLogShow = f;
- f.ShowFormLog = showFormLog;
- }
- f.Show();
- }
- else
- {
- _fff = new F_Log();
- _fff.Show();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnExePath_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
- }
- private void btnSetSetting_Click(object sender, EventArgs e)
- {
- try
- {
- SetSetting();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnGetSetting_Click(object sender, EventArgs e)
- {
- try
- {
- GetSetting();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- //private int PLC_S_T = 0;
- private void F_Main_Shown(object sender, EventArgs e)
- {
- try
- {
- this._windowState = this.WindowState;
- RefreshForm();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void RefreshForm()
- {
- if (string.IsNullOrWhiteSpace(PLC_S_S.ServiceName))
- {
- // 新服务设置
- ////PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{PLC_S_S.Port}]";
- ////PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{PLC_S_S.Port}]";
- //PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
- //PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
- //this.txtServiceName.Text = PLC_S_S.ServiceName;
- //this.txtDisplayName.Text = PLC_S_S.DisplayName;
- SetNSControl();
- return;
- }
- ServiceControllerStatus? wsStatus = null;
- if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
- {
- this.serviceController1.ServiceName = PLC_S_S.ServiceName;
- try
- {
- wsStatus = this.serviceController1.Status;
- }
- catch
- {
- // this.serviceController1.ServiceName = "";
- }
- }
- if (wsStatus == null)
- {
- // 非windows service
- if (this.chkAutoStart.Checked)
- {
- SetESControl();
- PLC_Server.Start();
- txtStatus.Text = (PLC_Server.ServerSocket?.Started ?? false ? "启动" : "停止");
- }
- else
- {
- SetNSControl();
- }
- }
- else
- {
- // windows service
- SetWSControl();
- this.txtStatus.Text = "WS:" + this.serviceController1.Status;
- }
- }
- /// <summary>
- /// WS 时,控件状态
- /// </summary>
- private void SetWSControl()
- {
- //PLC_S_T = 2;
- this.txtPort.ReadOnly = true;
- this.cboPLCST.Enabled = false;
- this.txtServiceName.ReadOnly = true;
- this.txtDisplayName.ReadOnly = true;
- this.txtDescription.ReadOnly = true;
- this.txtEPTS.ReadOnly = true;
- this.txtInterval.ReadOnly = true;
- this.btnStart.Enabled = false;
- this.btnStop.Enabled = false;
- this.btnLog.Enabled = false;
- this.btnWS_Insert.Enabled = false;
- this.btnWS_Delete.Enabled = true;
- this.btnWS_Start.Enabled = true;
- this.btnWS_Stop.Enabled = true;
- this.btnWS_ReStart.Enabled = true;
- this.btnWS_Refresh.Enabled = true;
- }
- /// <summary>
- /// es 时,控件状态
- /// </summary>
- private void SetESControl()
- {
- //PLC_S_T = 1;
- this.txtPort.ReadOnly = true;
- this.cboPLCST.Enabled = false;
- this.txtServiceName.ReadOnly = true;
- this.txtEPTS.ReadOnly = true;
- this.txtInterval.ReadOnly = true;
- this.txtDisplayName.ReadOnly = false;
- this.txtDescription.ReadOnly = false;
- this.btnStart.Enabled = false;
- this.btnStop.Enabled = true;
- this.btnLog.Enabled = true;
- this.btnWS_Insert.Enabled = false;
- this.btnWS_Delete.Enabled = false;
- this.btnWS_Start.Enabled = false;
- this.btnWS_Stop.Enabled = false;
- this.btnWS_ReStart.Enabled = false;
- this.btnWS_Refresh.Enabled = false;
- }
- /// <summary>
- /// 无服务时,控件状态
- /// </summary>
- private void SetNSControl()
- {
- //PLC_S_T = 0;
- this.txtPort.ReadOnly = false;
- this.cboPLCST.Enabled = true;
- this.txtServiceName.ReadOnly = false;
- this.txtEPTS.ReadOnly = false;
- this.txtInterval.ReadOnly = false;
- this.txtDisplayName.ReadOnly = false;
- this.txtDescription.ReadOnly = false;
- this.btnStart.Enabled = true;
- this.btnStop.Enabled = false;
- //this.btnLog.Enabled = false;
- this.btnWS_Insert.Enabled = true;
- this.btnWS_Delete.Enabled = false;
- this.btnWS_Start.Enabled = false;
- this.btnWS_Stop.Enabled = false;
- this.btnWS_ReStart.Enabled = false;
- this.btnWS_Refresh.Enabled = false;
- }
- private void F_Main_TextChanged(object sender, EventArgs e)
- {
- this.notifyIcon1.Text = this.Text;
- }
- private FormWindowState _windowState = FormWindowState.Normal;
- private bool _hasClose = false;
- private void F_Main_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Minimized)
- {
- //Visible = false;
- }
- else
- {
- this._windowState = this.WindowState;
- }
- }
- private void F_Main_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!_hasClose)
- {
- DialogResult dr = MessageBox.Show("是否退出?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3);
- if (dr == DialogResult.Cancel)
- {
- e.Cancel = true;
- //this.WindowState = FormWindowState.Minimized;
- return;
- }
- if (dr == DialogResult.No)
- {
- e.Cancel = true;
- notifyIcon1.Visible = true;
- Visible = false;
- return;
- }
- }
- notifyIcon1.Visible = false;
- SetSetting();
- }
- private void notifyIcon1_DoubleClick(object sender, EventArgs e)
- {
- }
- private void notifyIcon1_Click(object sender, EventArgs e)
- {
- //if (!this.Visible)
- //{
- Visible = true;
- //this.TopMost = true;
- //this.WindowState = FormWindowState.Normal;
- WindowState = this._windowState;
- ShowInTaskbar = true;
- //}
- Activate();
- }
- private void txtPort_TextChanged(object sender, EventArgs e)
- {
- //if (this.txtPort.Tag == null)
- //{
- // PLC_S_S.ServiceName = $"{PLC_S_S.S_Name}[{_SS_KEY}][{txtPort.Text}]";
- // PLC_S_S.DisplayName = $"{PLC_S_S.D_Name}[{_SS_KEY}][{txtPort.Text}]";
- // this.txtServiceName.Text = PLC_S_S.ServiceName;
- // this.txtDisplayName.Text = PLC_S_S.DisplayName;
- //}
- }
- private void btnWS_Refresh_Click(object sender, EventArgs e)
- {
- try
- {
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- this.txtStatus.Text = null;
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void RefreshWSStatus()
- {
- this.serviceController1.Refresh();
- this.txtStatus.Text = "WS:" + this.serviceController1.Status;
- Application.DoEvents();
- }
- private void txtDisplayName_TextChanged(object sender, EventArgs e)
- {
- this.Text = $"{PLC_S_S.DisplayName}";
- }
- private void txtPort_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (char.IsControl(e.KeyChar) ||
- char.IsNumber(e.KeyChar)
- )
- {
- return;
- }
- e.Handled = true;
- }
- private void txtPort_KeyDown(object sender, KeyEventArgs e)
- {
- //if (char.IsControl(e.KeyData))
- //{
- // e.Handled = true;
- //}
- }
- private void btnWS_Insert_Click(object sender, EventArgs e)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(this.txtPort.Text))
- {
- MessageBox.Show("PLC服务【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtPort.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(this.txtServiceName.Text))
- {
- MessageBox.Show("PLC服务【服务名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtServiceName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(this.txtDisplayName.Text))
- {
- MessageBox.Show("PLC服务【显示名】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtDisplayName.Focus();
- return;
- }
- SetSetting();
- string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
- InstallService(path);
- SetWSControl();
- this.serviceController1.ServiceName = PLC_S_S.ServiceName;
- if (PLC_S_S.AutoStart)
- {
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- }
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Delete_Click(object sender, EventArgs e)
- {
- try
- {
- if (IsServiceExisted(PLC_S_S.ServiceName.ToLower()))
- {
- string path = ApplicationInformation.GetAbsolutePath("PLC_WS.exe");
- UninstallService(path);
- }
- SetNSControl();
- this.txtStatus.Text = null;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Start_Click(object sender, EventArgs e)
- {
- try
- {
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_Stop_Click(object sender, EventArgs e)
- {
- try
- {
- this.serviceController1.Stop();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnWS_ReStart_Click(object sender, EventArgs e)
- {
- try
- {
- if (this.serviceController1.Status == ServiceControllerStatus.Running)
- {
- this.serviceController1.Stop();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- this.serviceController1.Start();
- RefreshWSStatus();
- this.serviceController1.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 1, 0));
- RefreshWSStatus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- /// <summary>
- /// 判断服务是否存在
- /// </summary>
- /// <param name="serviceName"></param>
- /// <returns></returns>
- private bool IsServiceExisted(string serviceName)
- {
- ServiceController[] services = ServiceController.GetServices();
- foreach (ServiceController sc in services)
- {
- if (sc.ServiceName.ToLower() == serviceName)
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 安装服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private void InstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- //IDictionary savedState = new Hashtable();
- //installer.Install(savedState);
- //installer.Commit(savedState);
- installer.Install(null);
- installer.Commit(null);
- }
- }
- /// <summary>
- /// 卸载服务
- /// </summary>
- /// <param name="serviceFilePath"></param>
- private void UninstallService(string serviceFilePath)
- {
- using (AssemblyInstaller installer = new AssemblyInstaller())
- {
- installer.UseNewContext = true;
- installer.Path = serviceFilePath;
- installer.Uninstall(null);
- }
- }
- /// <summary>
- /// 启动服务
- /// </summary>
- /// <param name="serviceName"></param>
- private void ServiceStart(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Stopped)
- {
- control.Start();
- }
- }
- }
- /// <summary>
- /// 停止服务
- /// </summary>
- /// <param name="serviceName"></param>
- private void ServiceStop(string serviceName)
- {
- using (ServiceController control = new ServiceController(serviceName))
- {
- if (control.Status == ServiceControllerStatus.Running)
- {
- control.Stop();
- }
- }
- }
- private void btnRefreshForm_Click(object sender, EventArgs e)
- {
- try
- {
- RefreshForm();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnGetDB_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- string ip = ini.Read("Oracle", "Server");
- string port = ini.Read("Oracle", "Port");
- string sid = ini.Read("Oracle", "Database");
- string username = ini.Read("Oracle", "UserName");
- string password = ini.Read("Oracle", "Password");
- if (!string.IsNullOrWhiteSpace(ip))
- {
- this.txtOracleName.Text = ip;
- }
- if (!string.IsNullOrWhiteSpace(port))
- {
- this.txtOraclePort.Text = port;
- }
- this.txtOracleDatabaseName.Text = sid;
- this.txtOracleUserName.Text = username;
- if (!string.IsNullOrEmpty(password))
- {
- try
- {
- password = SymmetricCryptogram.Decrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- this.txtOraclePassword.Text = password;
- }
- catch (Exception ex1)
- {
- this.txtOraclePassword.Text = null;
- MessageBox.Show("数据库【密码】解密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】解密错误");
- }
- }
- else
- {
- this.txtOraclePassword.Text = null;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnSetDB_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtOracleName.Text.Trim();
- string port = this.txtOraclePort.Text.Trim();
- string sid = this.txtOracleDatabaseName.Text.Trim();
- string username = this.txtOracleUserName.Text.Trim();
- string password = this.txtOraclePassword.Text;
- if (!string.IsNullOrEmpty(password))
- {
- try
- {
- password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- }
- catch (Exception ex1)
- {
- MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】加密错误");
- password = "";
- }
- }
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("Oracle", "Server", ip);
- ini.Write("Oracle", "Port", port);
- ini.Write("Oracle", "Database", sid);
- ini.Write("Oracle", "UserName", username);
- ini.Write("Oracle", "Password", password);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnOracleTest_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtOracleName.Text.Trim();
- string port = this.txtOraclePort.Text.Trim();
- string sid = this.txtOracleDatabaseName.Text.Trim();
- string username = this.txtOracleUserName.Text.Trim();
- string password = this.txtOraclePassword.Text;
- if (string.IsNullOrWhiteSpace(ip))
- {
- MessageBox.Show("数据库【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(port))
- {
- MessageBox.Show("数据库【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOraclePort.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(sid))
- {
- MessageBox.Show("数据库【实例】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleDatabaseName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(username))
- {
- MessageBox.Show("数据库【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOracleUserName.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(password))
- {
- MessageBox.Show("数据库【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtOraclePassword.Focus();
- return;
- }
- IDataAccess dataAccess = DataAccess.CreateByParameters(DataBaseType.Oracle, ip, port, sid, username, password);
- try
- {
- dataAccess.Open();
- dataAccess.ExecuteScalar("select sysdate from dual");
- }
- catch (Exception ex2)
- {
- MessageBox.Show("数据库【连接】错误" + System.Environment.NewLine + ex2.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex2, "数据库【连接】错误");
- return;
- }
- DialogResult dr = MessageBox.Show("数据库【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
- this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
- if (dr == DialogResult.Yes)
- {
- try
- {
- password = SymmetricCryptogram.Encrypt(password, "DongkeIbossprd", SymmetricCryptogramType.DES);
- }
- catch (Exception ex1)
- {
- MessageBox.Show("数据库【密码】加密错误" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "数据库【密码】加密错误");
- return;
- }
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("Oracle", "Server", ip);
- ini.Write("Oracle", "Port", port);
- ini.Write("Oracle", "Database", sid);
- ini.Write("Oracle", "UserName", username);
- ini.Write("Oracle", "Password", password);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void txtStatus_TextChanged(object sender, EventArgs e)
- {
- string t = txtStatus.Text;
- if (t.Contains("启动") || t.Contains("Running"))
- {
- txtStatus.ForeColor = Color.Blue;
- }
- else if (t.Contains("停止") || t.Contains("Stopped"))
- {
- txtStatus.ForeColor = Color.Red;
- }
- else
- {
- txtStatus.ForeColor = Color.Yellow;
- }
- }
- private void btnGetMES_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- string ip = ini.Read("iBOSS.MES", "IP");
- string port = ini.Read("iBOSS.MES", "Port");
- string ServiceName = ini.Read("iBOSS.MES", "ServiceName");
- string account = ini.Read("iBOSS.MES", "AccountCode");
- string username = ini.Read("iBOSS.MES", "UserCode");
- string password = ini.Read("iBOSS.MES", "Password");
- if (!string.IsNullOrWhiteSpace(ip))
- {
- this.txtMES_IP.Text = ip;
- }
- this.txtMES_IP.Tag = ServiceName;
- if (!string.IsNullOrWhiteSpace(port))
- {
- this.txtMES_Port.Text = port;
- }
- this.txtMES_Account.Text = account;
- this.txtMES_User.Text = username;
- this.txtMES_Password.Text = password;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnSetMES_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtMES_IP.Text.Trim();
- string port = this.txtMES_Port.Text.Trim();
- string account = this.txtMES_Account.Text.Trim();
- string username = this.txtMES_User.Text.Trim();
- string password = this.txtMES_Password.Text;
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("iBOSS.MES", "IP", ip);
- ini.Write("iBOSS.MES", "Port", port);
- ini.Write("iBOSS.MES", "AccountCode", account);
- ini.Write("iBOSS.MES", "UserCode", username);
- ini.Write("iBOSS.MES", "Password", password);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnMESTest_Click(object sender, EventArgs e)
- {
- try
- {
- string ip = this.txtMES_IP.Text.Trim();
- string ServiceName = this.txtMES_IP.Tag + "";
- string port = this.txtMES_Port.Text.Trim();
- string account = this.txtMES_Account.Text.Trim();
- string username = this.txtMES_User.Text.Trim();
- string password = this.txtMES_Password.Text;
- if (string.IsNullOrWhiteSpace(ip))
- {
- MessageBox.Show("MES【地址】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_IP.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(ServiceName))
- {
- MessageBox.Show("MES【服务】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_IP.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(port))
- {
- MessageBox.Show("MES【端口】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_Port.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(account))
- {
- MessageBox.Show("MES【帐套】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_Account.Focus();
- return;
- }
- if (string.IsNullOrWhiteSpace(username))
- {
- MessageBox.Show("MES【用户】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- this.txtMES_User.Focus();
- return;
- }
- //if (string.IsNullOrWhiteSpace(password))
- //{
- // MessageBox.Show("MES【密码】不能为空", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- // this.txtMES_Password.Focus();
- // return;
- //}
- try
- {
- WCFProxySetting ps = new WCFProxySetting(ip, port, ServiceName);
- SmartDeviceProxy smartDevice = new SmartDeviceProxy(ps);
- string t = smartDevice.Invoke<string>(p => p.Test("text"));
- // TODO
- }
- catch (Exception ex1)
- {
- MessageBox.Show("MES【连接】失败" + System.Environment.NewLine + ex1.Message,
- this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
- logger.OutputError(ex1, "MES【连接】失败");
- return;
- }
- DialogResult dr = MessageBox.Show("MES【连接】成功" + System.Environment.NewLine + "【是】【否】保存设置?",
- this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
- if (dr == DialogResult.Yes)
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("iBOSS.MES", "IP", ip);
- ini.Write("iBOSS.MES", "Port", port);
- ini.Write("iBOSS.MES", "AccountCode", account);
- ini.Write("iBOSS.MES", "UserCode", username);
- ini.Write("iBOSS.MES", "Password", password);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void txtStatus_KeyPress(object sender, KeyPressEventArgs e)
- {
- e.Handled = true;
- }
- private bool _isEPTS = false;
- private string _SS_KEY = null;
- private void cboPLCST_SelectedIndexChanged(object sender, EventArgs e)
- {
- try
- {
- string key = cboPLCST.SelectedValue + "";
- if (key.StartsWith("EPTS_"))
- {
- this.lblPort.Text = "设备";
- //_SS_KEY = key.Substring(5);
- _SS_KEY = key;
- }
- else
- {
- this.lblPort.Text = "端口";
- _SS_KEY = key;
- }
- if (this.txtPort.Tag != null)
- {
- return;
- }
- if (key.StartsWith("EPTS_"))
- {
- _isEPTS = true;
- this.txtPort.Text = null;
- }
- else
- {
- if (_isEPTS)
- {
- _isEPTS = false;
- this.txtPort.Text = null;
- }
- this.txtPort_TextChanged(null, null);
- }
- //if (cboPLCST.SelectedItem is DataRowView ddd)
- //{
- // this.txtDescription.Text = ddd["description"] + "";
- //}
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- public static int PLC_LOG_LEN = 5000;
- private void btnLogLenR_Click(object sender, EventArgs e)
- {
- try
- {
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- PLC_LOG_LEN = ini.Read("PLC_WF", "LOG_LEN").ToInt32();
- if (PLC_LOG_LEN < 1000)
- {
- PLC_LOG_LEN = 1000;
- }
- txtLogLen.Text = PLC_LOG_LEN+"";
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnLogLenW_Click(object sender, EventArgs e)
- {
- try
- {
- int len = txtLogLen.Text.ToInt32();
- if (len < 1000)
- {
- len = 1000;
- }
- PLC_LOG_LEN = len;
- txtLogLen.Text = PLC_LOG_LEN + "";
- INIHelper ini = INIHelper.Create($@"PLC_S_INI\PLC_S_S.ini");
- ini.Write("PLC_WF", "LOG_LEN", txtLogLen.Text);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnPLC_CRW_Click(object sender, EventArgs e)
- {
- try
- {
- //string S_NAME = "SSS_1";
- //Logger logger = Logger.CreateLogger(S_NAME+ "-S");
- //Logger logger_t = Logger.CreateLogger(S_NAME + "-T");
- //Logger logger_e = Logger.CreateLogger(S_NAME + "-E");
- //Logger logger_e_out = Logger.CreateLogger(S_NAME + "-EO");
- //logger.FileNameWithoutDate = true;
- //logger.FileExistDays = 30;
- //logger.LevelOneFile = true;
- //logger.FilePrefix = null;
- //logger.FileSuffix = logger.Name;
- //logger_t.FileExistDays = 30;
- //logger_t.LevelOneFile = true;
- //logger_t.SubFolderFormat = logger_t.Name;
- //logger_e.FileExistDays = 0;
- //logger_e_out.FileExistDays = 0;
- //logger_e_out.FilePrefix = null;
- //logger_e_out.FileSuffix = S_NAME;
- //logger_e_out.Directory = "..\\_PLC_Logs";
- //logger.OutputTrace("S_start");
- //logger_t.OutputTrace("S_start");
- //logger_t.OutputWarn("S_start");
- //logger_e.OutputWarn("S_start");
- //logger_e_out.OutputWarn("S_start");
- //logger_e.OutputError(null,"S_start");
- //logger_e_out.OutputError(null,"S_start");
- switch (cboPLCType.Text)
- {
- case "MelsecA1EAscii":
- F_PLC_M<MelsecA1EAsciiModel> fm = new F_PLC_M<MelsecA1EAsciiModel>();
- fm.Show();
- break;
- case "SiemensS7_1200":
- F_PLC_M<SiemensS7_1200Model> fs = new F_PLC_M<SiemensS7_1200Model>();
- fs.Show();
- break;
- case "SiemensS7_200Smart":
- F_PLC_M<SiemensS7_200SmartModel> fss = new F_PLC_M<SiemensS7_200SmartModel>();
- fss.Show();
- break;
- case "SimpleSocketClientModel":
- F_PLC_M<SimpleSocketClientModel> fs1 = new F_PLC_M<SimpleSocketClientModel>();
- fs1.Show();
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- //取得服务的启动类型的当前状态,返回启动类型的状态値
- //2:自动,3:手动,4:禁用 ......
- //sServiceName服务名称
- //sState输出相关的状态代码,失败则为错误消息
- public int GetWindowsServiceStartType(string sServiceName )
- {
- string sState = "";
- try
- {
- System.Diagnostics.ProcessStartInfo objProcessInf = new System.Diagnostics.ProcessStartInfo();
- objProcessInf.FileName = "cmd.exe";
- objProcessInf.UseShellExecute = false;
- objProcessInf.RedirectStandardError = true;
- objProcessInf.RedirectStandardInput = true;
- objProcessInf.RedirectStandardOutput = true;
- objProcessInf.CreateNoWindow = true;
- objProcessInf.Arguments = "/c sc qc " + sServiceName;
- System.Diagnostics.Process objProcess = System.Diagnostics.Process.Start(objProcessInf);
- String sStateValue = objProcess.StandardOutput.ReadToEnd();
- if (sStateValue.IndexOf("AUTO_START") > 0)
- {
- sState = "AUTO_START";
- return 1;
- }
- if (sStateValue.IndexOf("DEMAND_START") > 0)
- {
- sState = "DEMAND_START";
- return 2;
- }
- if (sStateValue.IndexOf("DISABLED") > 0)
- {
- sState = "DISABLED";
- return 3;
- }
- return 0;
- }
- catch (Exception e)
- {
- sState = e.Message;
- return -1;
- }
- }
- void KillProcess(string MainWindowTitle)
- {
- //productName = productName.ToLower();
- Process[] allProcess = Process.GetProcesses();
- foreach (Process p in allProcess)
- {
- if(p.MainWindowTitle.StartsWith(MainWindowTitle, true, null))
- //if (p.ProcessName.ToLower().StartsWith(processname))
- {
- try
- {
- for (int i = 0; i < p.Threads.Count; i++)
- {
- p.Threads[i].Dispose();
- }
- }
- catch (Exception ex)
- {
- //Logger.Error(ex);
- }
- try
- {
- p.Kill();
- }
- catch (Exception ex)
- {
- //Logger.Error(ex);
- }
- //break;
- }
- }
- }
- private void btnStopAll_Click(object sender, EventArgs e)
- {
- //KillProcess("Finance");
- //return;
- try
- {
- List<string> mms = new List<string>();
- // 获取系统服务列表
- ServiceController[] services = ServiceController.GetServices();
- // 遍历系统服务列表
- foreach (ServiceController service in services)
- {
- // 服务名称
- string name = service.ServiceName;
- try
- {
- if (name.StartsWith("DK.PLC["))
- {
- if (service.Status == ServiceControllerStatus.Running)
- {
- //int ss = GetWindowsServiceStartType(name);
- //if (ss == 1)
- //{
- // service.Start();
- //}
- service.Stop();
- }
- }
- }
- catch (Exception ex)
- {
- mms.Add(name + "[" + ex.Message + "]");
- }
- }
- if (mms.Count > 0)
- {
- MessageBox.Show(string.Join("; ", mms), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnStartAll_Click(object sender, EventArgs e)
- {
- try
- {
- List<string> mms = new List<string>();
- // 获取系统服务列表
- ServiceController[] services = ServiceController.GetServices();
- // 遍历系统服务列表
- foreach (ServiceController service in services)
- {
- // 服务名称
- string name = service.ServiceName;
- try
- {
- if (name.StartsWith("DK.PLC["))
- {
- if (service.Status == ServiceControllerStatus.Stopped)
- {
- int ss = GetWindowsServiceStartType(name);
- if (ss == 1)
- {
- service.Start();
- }
- }
- }
- }
- catch (Exception ex)
- {
- mms.Add(name + "[" + ex.Message + "]");
- }
- }
- if (mms.Count > 0)
- {
- MessageBox.Show(string.Join("; ", mms), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
|