基于TCPIP在线更新文件客户端
int uptype = 2;//1 客户端 2管理端
Thread threadClient = null; // 创建用于接收服务端消息的 线程;
Socket sockClient = null;
DispatcherTimer timer = new DispatcherTimer();
DispatcherTimer timer2 = new DispatcherTimer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//bool status = false;
连接共享目录
//status = connectState(@"\192.168.0.112\up", “”, “”);
//if (status)
//{
// //共享文件夹的目录
// DirectoryInfo theFolder = new DirectoryInfo(@"\192.168.0.112\up");
// string filename = theFolder.ToString();
// FileVersionInfo yuninfo = FileVersionInfo.GetVersionInfo(filename + "DAL.dll");// //执行方法// FileVersionInfo info = FileVersionInfo.GetVersionInfo(AppDomain.CurrentDomain.BaseDirectory + "DAL.dll");// if (info.ProductVersion != yuninfo.ProductVersion)// {// if (File.Exists(filename + "DAL.dll"))//必须判断要复制的文件是否存在// {// File.Copy(filename + "DAL.dll", AppDomain.CurrentDomain.BaseDirectory + "DAL.dll", true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换// }// //TransportRemoteToLocal(AppDomain.CurrentDomain.BaseDirectory + "DAL.dll", filename, "DAL.dll"); //实现将远程服务器文件写入到本地// }//}//else//{// Console.WriteLine("未能连接!");//}//Console.WriteLine("成功");// Console.ReadKey();var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;this.Left = desktopWorkingArea.Right - this.Width;this.Top = desktopWorkingArea.Bottom - this.Height;IPAddress ip = IPAddress.Parse("138.1.1.88");//49.235.143.221IPEndPoint endPoint = new IPEndPoint(ip, int.Parse("7912"));sockClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);try{ShowMsg("与服务器连接中……");sockClient.Connect(endPoint);}catch (SocketException se){MessageBox.Show(se.Message);return;}ShowMsg("检查更新信息请稍等....");threadClient = new Thread(RecMsg);threadClient.IsBackground = true;threadClient.Start();timer.Interval = TimeSpan.FromMilliseconds(3000);timer.Tick += Timer_Tick;timer.Start();timer2.Interval = TimeSpan.FromMilliseconds(5000);timer2.Tick += Timer2_Tick;}/// <summary>/// 连接远程共享文件夹/// </summary>/// <param name="path">远程共享文件夹的路径</param>/// <param name="userName">用户名</param>/// <param name="passWord">密码</param>/// <returns></returns>public static bool connectState(string path, string userName, string passWord){bool Flag = false;Process proc = new Process();try{proc.StartInfo.FileName = "cmd.exe";proc.StartInfo.UseShellExecute = false;proc.StartInfo.RedirectStandardInput = true;proc.StartInfo.RedirectStandardOutput = true;proc.StartInfo.RedirectStandardError = true;proc.StartInfo.CreateNoWindow = true;proc.Start();string dosLine = "net use " + path + " " + passWord + " /user:" + userName;proc.StandardInput.WriteLine(dosLine);proc.StandardInput.WriteLine("exit");while (!proc.HasExited){proc.WaitForExit(1000);}string errormsg = proc.StandardError.ReadToEnd();proc.StandardError.Close();if (string.IsNullOrEmpty(errormsg)){Flag = true;}else{throw new Exception(errormsg);}}catch (Exception ex){throw ex;}finally{proc.Close();proc.Dispose();}return Flag;}public static bool connectState(string path){return connectState(path, "", "");}/// <summary>
/// 从远程服务器下载文件到本地
/// </summary>
/// <param name="src">下载到本地后的文件路径,包含文件的扩展名</param>
/// <param name="dst">远程服务器路径(共享文件夹路径)</param>
/// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param>
public static void TransportRemoteToLocal(string src, string dst, string fileName) //src:下载到本地后的文件路径 dst:远程服务器路径 fileName:远程服务器dst路径下的文件名{if (!Directory.Exists(dst)){Directory.CreateDirectory(dst);}dst = dst + fileName;FileStream inFileStream = new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate); //从远程服务器下载到本地的文件byte[] buf = new byte[inFileStream.Length];int byteCount;while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0){outFileStream.Write(buf, 0, byteCount);}inFileStream.Flush();inFileStream.Close();outFileStream.Flush();outFileStream.Close();}private void Timer2_Tick(object sender, EventArgs e){this.Close();}void qidong() {Process p = new System.Diagnostics.Process();p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + (uptype==1? "员工学习智能推送平台-员工端.exe" : "员工学习智能推送平台-管理端.exe");//学习系统管理端 农业发展银行学习系统p.StartInfo.Arguments = "";p.StartInfo.UseShellExecute = false;p.StartInfo.RedirectStandardInput = true;p.StartInfo.RedirectStandardOutput = true;p.StartInfo.RedirectStandardError = true;p.StartInfo.CreateNoWindow = false;p.Start();}int updown = 0;bool downwc = true;private void Timer_Tick(object sender, EventArgs e){if (downwc){if (uplist.Count > 0){if (updown == uplist.Count){timer2.Start();btwc.Content = "完成";btwc.IsEnabled = true;ShowMsg("软件更新完成 重启中.....");timer.Stop();return;}updatefiles(uplist[updown]);downwc = false;updown++;}else{timer2.Start();btwc.Content = "完成";btwc.IsEnabled = true;ShowMsg("无需更新 重启中.....");timer.Stop();return;}}else{ShowMsg("更新中请稍后...");}}string filename = "";void RecMsg(){while (true){// 定义一个2M的缓存区;byte[] arrMsgRec = new byte[1024 * 1024 * 100];// 将接受到的数据存入到输入 arrMsgRec中;int length = -1;try{length = sockClient.Receive(arrMsgRec); // 接收数据,并返回数据的长度;}catch (SocketException se){ShowMsg("异常;" + se.Message);return;}catch (Exception e){ShowMsg("异常:" + e.Message);return;}if (arrMsgRec[0] == 0) // 表示接收到的是消息数据;{string strMsg = System.Text.Encoding.UTF8.GetString(arrMsgRec, 1, length - 1);// 将接受到的字节数据转化成字符串;//ShowMsg(strMsg);if (strMsg.Contains("gx:")){uplistfile(strMsg.Replace("gx:", ""));}if (strMsg.Contains("filename:")){filename = strMsg.Replace("filename:", "");}}if (arrMsgRec[0] == 1) // 表示接收到的是文件数据; 更新文件{new Thread(() => {Dispatcher.Invoke(new Action(() =>{// SaveFileDialog sfd = new SaveFileDialog();// if (sfd.ShowDialog(this) == true)// {// 在上边的 sfd.ShowDialog() 的括号里边一定要加上 this 否则就不会弹出 另存为 的对话框,而弹出的是本类的其他窗口,,这个一要注意!!!【解释:加了this的sfd.ShowDialog(this),“另存为”窗口的指针才能被SaveFileDialog的对象调用,若不加thisSaveFileDialg 的对象调用的是本类的其他窗口了,当然不弹出“另存为”窗口。】// string fileSavePath = sfd.FileName;// 获得文件保存的路径;// 创建文件流,然后根据路径创建文件;if (filename != ""){string fileSavePath = AppDomain.CurrentDomain.BaseDirectory + filename;using (FileStream fs = new FileStream(fileSavePath, FileMode.Create)){fs.Write(arrMsgRec, 1, length - 1);downwc = true;// ShowMsg("文件保存成功:" + fileSavePath);}}// }}));}).Start();}}}void ShowMsg(string str){new Thread(() => {Dispatcher.Invoke(new Action(() =>{txtMsg.AppendText(str + "\r\n");}));}).Start();}List<string> uplist = new List<string>() { };/// <summary>/// 更新的文件/// </summary>/// <param name="daifile"></param>void uplistfile(string daifile) {//待验证string[] df= daifile.Split(',');List<string> ln = new List<string>() { };foreach (string item in df){string[] bb = item.Split('|');string filename = bb[0];string ver = bb[1];if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + filename)){FileVersionInfo info = FileVersionInfo.GetVersionInfo(AppDomain.CurrentDomain.BaseDirectory + filename);if (info.ProductVersion != ver){//更新客户端 银行 更新管理端 管理if (!filename.Contains((uptype == 1 ? "管理" : "员工端"))){uplist.Add(filename);}}}else{if (!filename.Contains((uptype == 1 ? "管理" : "员工端"))){uplist.Add(filename);}}}}/// <summary>/// 发送命令 下载更新文件/// </summary>/// <param name="filename">文件名</param>void updatefiles(string filename){string strMsg = filename;//"123.avi";byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);byte[] arrSendMsg = new byte[arrMsg.Length + 1];arrSendMsg[0] = 0; // 用来表示发送的是消息数据Buffer.BlockCopy(arrMsg, 0, arrSendMsg, 1, arrMsg.Length);sockClient.Send(arrSendMsg); // 发送消息;//ShowMsg(strMsg);}
基于TCPIP在线更新文件客户端相关推荐
- Linux平台基于socket的文件传输服务器和客户端
Linux平台基于socket的文件传输服务器和客户端 目录 前言 一.服务器程序结构 二.客户程序结构 三.代码 1.服务器主程序video_serv_fork.c 2.服务器子程序video_tr ...
- Linux网络编程 - 基于UDP的服务器端/客户端
一 理解UDP 1.0 UDP协议简介 UDP(User Datagram Protocol,用户数据报协议) [RFC 768] UDP协议的数据传输单元叫 UDP用户数据报,而TCP协议的数据传输 ...
- 基于diff的文件同步算法(上)
1.写在前面 文件同步的核心问题是新旧版本的对比问题.这里所说的版本是指用于区分文件变更的一种状态标记,它可以是我们通常所说的版本号,也可以是时间戳,如修改时间.本文介绍一种基于diff的文件同步算法 ...
- 基于Socket的文件传输(使用CSocket类)
本软件使用MFC采用面向对象的方法实现了基于Socket的文件传输.这是原来研究生课程的结课作业,实现了Socket的发送和接收,以及读取ini配置文件等操作.使用了CSocket类 以下是当时结课作 ...
- 基于Web的文件管理系统,支持Office、WPS预览/编辑、在线解压缩、文件分享、文件加密、远程存储、远程文件推送、秒传、断点
基于Web的文件管理系统,支持权限管理.历史版本管理.Office预览/编辑.WPS预览/编辑.在线解压缩.文件分享.文件加密.远程存储.远程文件推送.秒传.断点续传.智能搜索.文件备注.本地自动备份 ...
- 《基于Android微博整合客户端的设计与实现》毕业设计论文任务书
华 南 理 工 大 学 广 州 学 院 毕 业 设 计 (论文) 任 务 书 兹发给计算机科学与技术专业 1班学生巫文杰毕业设计(论文)任务书,内容如下: 1.毕业设计(论文)题目:基于Android ...
- html标签手册 360doc,基于AJAX的文件上传控件NetAdvantage for jQuery
NetAdvantage for jQuery 是一款全新的轻量级.高性能的jQuery控件,包含了在线的Video播放控件,基于AJAX的文件上传控件,快速且强大的表格控件,以及创建和编辑Word. ...
- 基于rt-thread的udp客户端
基于rt-thread的udp客户端 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 开发环境:MDK5.23 rt-thread版本:2.1. ...
- TCP/IP网络编程_第6章基于UDP的服务器端/客户端
6.1 理解 DUP 我们在第4章学习TCP的过程中, 还同时了解了 TCP/IP 协议. 在4层TCP/IP模型中, 上数第二层传输(Transport)层分为TCP和UDP这两种. 数据交换过程可 ...
最新文章
- vue webpack打包入口文件是哪个_Vue项目起步
- springboot redis
- Java设计模式:工厂模式
- Redis主从配置及通过Keepalived实现Redis自动切换高可用
- Ubuntu16.04 安装ftp
- c语言hellowwo所占字节数,哪个懂C语言?帮忙做~个题,跪求
- 科学计算机乱码,软件界面乱码可以这么“破”
- Linux之RPM 软件管理程序
- JSON用于多态Java对象序列化
- u盘插在linux电脑上没有反应,U盘插在自己电脑上没有反应,别人电脑有反应的解决方法...
- DRUID连接池的简单使用
- leetcode259. 较小的三数之和
- 深入 JavaScript 装饰器
- 自动驾驶路径规划论文解析(3)
- python `__slots__`
- 深入分析redis cluster 集群安装配置详解
- mysql4ge表联表查询_【MySQL】MariaDB10.2新特性--Flashback
- 【原创】大叔案例分享(4)定位分析--见证scala的强大
- Win32 Application和Win32 Console Application区别
- 怎么自己制作证件照?如何在线制作证件照电子版?