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;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值