您的位置:首页 > 游戏 > 游戏 > C#编写软件发布公告1——客户端

C#编写软件发布公告1——客户端

2024/9/24 23:30:46 来源:https://blog.csdn.net/weixin_45114627/article/details/140765925  浏览:    关键词:C#编写软件发布公告1——客户端

前言
软件或者生活中有时需要将信息同步至电子公告板上,利用C#可以快速实现这一目的,这里以软件公告场景设计,主要是将软件的版本号等相关信息同步至服务器,同步成功后,任务需要查找的人员只要有Web浏览器就可以快速查看更新信息;如果在社区公告场景也可以如法炮制

一、界面效果展示

1、启动界面

在这里插入图片描述

2、后台配置文件

在这里插入图片描述

3、读取数据

在这里插入图片描述

4、上传

客户端操作:
在这里插入图片描述

5、结果

服务器显示:
在这里插入图片描述
在这里插入图片描述

二、实际代码

注意: 这里只是列举主要逻辑代码。

1、Web访问类

利用C#提供的HttpWebRequest来进行Web访问(由于是CS这种结构,所以这个服务端也可以用其他语言进行编写,同理客户端也可以是多种语言进行编写),根据实际情况封装Post和Get请求。

 private string Post(string str, string url){string result = "";HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url+"?"+str);httpWebRequest.Method = "POST";httpWebRequest.ContentType = "application/json; charset=UTF-8";httpWebRequest.Timeout = 5000;//byte[] bytes = Encoding.UTF8.GetBytes(str);//httpWebRequest.ContentLength = (long)bytes.Length;using (StreamReader streamReader = new StreamReader(((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream(), Encoding.UTF8)){result = streamReader.ReadToEnd();}return result;}private string Get(string url){// ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MesCommHelp.CheckValidationResult);string result = "";HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);httpWebRequest.Method = "GET";httpWebRequest.ContentType = "application/json; charset=UTF-8";httpWebRequest.Timeout = 2000;using (StreamReader streamReader = new StreamReader(((HttpWebResponse)httpWebRequest.GetResponse()).GetResponseStream(), Encoding.UTF8)){result = streamReader.ReadToEnd();}return result;}

2、Ini配置读写类

 public string INIGetStringValue(string section, string key, string defaultValue){string value = defaultValue;const int SIZE = 1024;if (string.IsNullOrEmpty(section)){throw new ArgumentException("必须指定节点名称", "section");}if (string.IsNullOrEmpty(key)){throw new ArgumentException("必须指定键名称(key)", "key");}StringBuilder sb = new StringBuilder(SIZE);uint bytesReturned = GetPrivateProfileString(section, key, defaultValue, sb, SIZE, _iniFile);if (bytesReturned != 0){value = sb.ToString();}sb = null;return value;}public bool INIWriteValue(string section, string key, string value){if (string.IsNullOrEmpty(section)){throw new ArgumentException("必须指定节点名称", "section");}if (string.IsNullOrEmpty(key)){throw new ArgumentException("必须指定键名称", "key");}if (value == null){throw new ArgumentException("值不能为null", "value");}return WritePrivateProfileString(section, key, value, _iniFile);}

3、业务中心类

1、配置信息读取

        private void ReadInfo(){BaseUrl = iniHelp.INIGetStringValue("MESInfo", "URL", "");LastSn = iniHelp.INIGetStringValue("LastRecord", "SN", "");LastDataTime = iniHelp.INIGetStringValue("LastRecord", "Time", "");RecordFileName = iniHelp.INIGetStringValue("FileInfo", "Path", "");EndSymbol = iniHelp.INIGetStringValue("FileInfo", "EndSymbol", "EndSymbol");}

2、公告文件读取

 public bool ReadFileInfo(out InfoItem infoItem){infoItem = new InfoItem();try{string[] allLines = File.ReadAllLines(RecordFileName);int i = allLines.Length - 1;bool IsHave = false;int endIndex = -1;for (; i > 0; i--){if (string.IsNullOrEmpty(allLines[i])){if (IsHave){break;}}else{if (!IsHave){IsHave = true;endIndex = i;}}}i = i + 1;string firstRow = allLines[i];string descInfo = string.Empty;for (int j = i + 1; j <= endIndex; j++){string item = allLines[j];if (!item.EndsWith(EndSymbol)){item += EndSymbol;}string[] arrs= item.Split('、');if (arrs.Length>1){if (int.TryParse( arrs[0],out int id)){Console.WriteLine("切割后处理:" + arrs[0]);item = item.Replace(arrs[0] + "、", arrs[0] + ".");}}descInfo += item;}string[] strs = firstRow.Split(' ');infoItem = new InfoItem(strs[1], strs[0], descInfo);return true;}catch (System.Exception ex){ErrStr = ex.Message;return false;}}

3、保存更新记录

 private void UpdataRecordToIni(string sn){iniHelp.INIWriteValue("LastRecord", "SN", sn);iniHelp.INIWriteValue("LastRecord", "Time", DateTime.Now.ToString());}

4、提交更新信息

  internal bool UpdataRecord(InfoItem infoItem){string info = $"SN={infoItem.SN}&Datetime={infoItem.DateTime}&Info={ infoItem.DescrInfo}";try{string res = mes.ExePost(info, BaseUrl);if (res.Contains("成功")){UpdataRecordToIni(infoItem.SN);return true;}ErrStr = "服务器响应消息为失败";return false;}catch (System.Exception ex){ErrStr = ex.Message;return false;}}

5、信息检测

  private bool CheckInfo(string sn, string dateInfo, string str){if (string.IsNullOrEmpty(sn)){SetInfo("版本号不能为空", false);return false;}if (!Regex.IsMatch(sn, "^(?<y>\\d{1,2})\\.(?<m>\\d{1,2})\\.(?<d>\\d{1,2})$")){SetInfo("版本号格式不正确", false);return false;}if (string.IsNullOrEmpty(dateInfo)){SetInfo("日期不能为空", false);return false;}if (!Regex.IsMatch(dateInfo, "^(?<year>\\d{2,4})-(?<month>\\d{1,2})-(?<day>\\d{1,2})$")){SetInfo("日期格式不正确", false);return false;}if (string.IsNullOrEmpty(str)){SetInfo("描述信息不能为空", false);return false;}return true;}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com