在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#文件解析和处理 → 局域网文件定时传输V1.2版源代码

局域网文件定时传输V1.2版源代码

C#文件解析和处理

下载此实例
  • 开发语言:C#
  • 实例大小:2.06M
  • 下载次数:66
  • 浏览次数:507
  • 发布时间:2018-11-04
  • 实例类别:C#文件解析和处理
  • 发 布 人:sdm68
  • 文件格式:.zip
  • 所需积分:10
 相关标签: 局域网 文件 定时 2 源代码

实例介绍

【实例简介】多线程局域网文件定时传出,客户端与服务端结合,定时删除指定时间之前文件。


【实例截图】

 

from clipboard

【核心代码】


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using Microsoft.Win32;
using System.IO;


namespace NetServer
{
    public partial class frm_NetServer_Main : Form
    {


        #region 定义函数的委托
        /// <summary>
        /// 定义监听函数委托
        /// </summary>
        public delegate void D_ThreadRec();

        /// <summary>
        /// 定义利用委托显示界面信息
        /// </summary>
        /// <param name="str"></param>
        public delegate void D_UpdateUI_Log(string str);

        #endregion


        #region 定义公共变量
        /// <summary>
        /// 是否监听
        /// </summary>
        private bool isLis;
        /// <summary>
        /// 定义一个连接
        /// </summary>
        Socket ServerSocket;
        #endregion

        /// <summary>
        /// 定义个一个属性来修改ListBox_LogMsg控件,显示信息
        /// </summary>
        public string SetListBoxMsg
        {
            set
            {
                this.ListBox_LogMsg.Items.Add(value);
                ListBox_LogMsg.TopIndex = ListBox_LogMsg.Items.Count - 1;
            }
        }


        public frm_NetServer_Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置文件保存目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_SaveBrowser_Click(object sender, EventArgs e)
        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                Text_SavePath.Text = folderBrowserDialog1.SelectedPath.ToString();
                StartListent.Enabled = true;
            }
        }

        /// <summary>
        /// 启动监听服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartListent_Click(object sender, EventArgs e)
        {
            StartListentServer();
        }
        /// <summary>
        /// 启动监听服务
        /// </summary>
        public void StartListentServer()
        {

            isLis = true;
            Bt_SaveBrowser.Enabled = false;
            StopListent.Enabled = true;
            StartListent.Enabled = false;
            DelFileTime.Enabled = false;
            Check_SaveFileAS.Enabled = false;
            Text_DelFileTime.Enabled = false;
            Text_ServerIP.Enabled = false;
            Text_ServerPort.Enabled = false;

            if (Check_SaveFileAS.Checked)
            {
                ListBox_LogMsg.Items.Add("********文件会按接收的时间保存,并只保留近"   Text_DelFileTime.Value.ToString()   "天的文件********");
            }
            else
            {
                ListBox_LogMsg.Items.Add("********文件会按传输的原始文件名保存,并覆盖已存在的同名文件********");
            }

            Control.CheckForIllegalCrossThreadCalls = false;

            D_ThreadRec _D_ThreadRec = new D_ThreadRec(ThreadRec);
            _D_ThreadRec.BeginInvoke(null, null);

        }



        /// <summary>
        /// 停止监听服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StopListent_Click(object sender, EventArgs e)
        {
            isLis = false;
            Check_SaveFileAS.Enabled = true;
            Text_DelFileTime.Enabled = true;
            DelFileTime.Enabled = true;
            Text_ServerIP.Enabled = true;
            Text_ServerPort.Enabled = true;
            ServerSocket.Close();

            Bt_SaveBrowser.Enabled = true;

            StopListent.Enabled = false;

            if (Text_SavePath.Text != "")
            {
                StartListent.Enabled = true;
            }
        }

        #region 启动监听服务
        /// <summary>
        /// 监听服务函数
        /// </summary>
        private void ThreadRec()
        {
            try
            {
                int port = Convert.ToInt32(Text_ServerPort.Value);
                string strIp = Text_ServerIP.Text;

                IPAddress ip = IPAddress.Parse(strIp);
                IPEndPoint ipe = new IPEndPoint(ip, port);

                ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                ServerSocket.Bind(ipe);
                ServerSocket.Listen(10);

                this.BeginInvoke(new D_UpdateUI_Log(UpdateUI_Log), new object[] { System.DateTime.Now   ":服务器开始监听"   Text_ServerPort.Value.ToString()  "端口!" });
                while (true)
                {
                    // ListBox_LogMsg.Items.Add("开始监听!");
                    if (isLis == true)
                    {
                        try
                        {
                            //阻塞监听至到有新的连接
                            Socket temp = ServerSocket.Accept();

                            IPEndPoint clientip = (IPEndPoint)temp.RemoteEndPoint;
                            //如果连接上
                            if (temp.Connected)
                            {
                                //显示客户端连接状态
                                this.BeginInvoke(new D_UpdateUI_Log(UpdateUI_Log), new object[] { "客户端["   clientip.Address.ToString()   "]在"   System.DateTime.Now   "连接到服务器!" });
                                StartThreads startThreads = new StartThreads(temp, this, Text_SavePath.Text.Trim(), Check_SaveFileAS.CheckState.ToString());
                                //.....................
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.Message != "一个封锁操作被对 WSACancelBlockingCall 的调用中断。")//断开socket时阻塞监听的报错提示
                            {
                                MessageBox.Show(ex.Message);
                            }

                        }
                    }
                    else
                    {

                        ServerSocket.Close();
                        this.BeginInvoke(new D_UpdateUI_Log(UpdateUI_Log), new object[] { System.DateTime.Now   ":停止监听服务!" });
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                this.BeginInvoke(new D_UpdateUI_Log(UpdateUI_Log), new object[] { ex.Message });
            }
        }
       

        #endregion


        /// <summary>
        /// 更新界面信息
        /// </summary>
        /// <param name="str"></param>
        public void UpdateUI_Log(string str)
        {
            ListBox_LogMsg.Items.Add(str);
            ListBox_LogMsg.TopIndex = ListBox_LogMsg.Items.Count - 1;
            if (ListBox_LogMsg.Items.Count > 200)
            {
                ListBox_LogMsg.Items.Clear();
            }
        }

        private void frm_NetServer_Main_Load(object sender, EventArgs e)
        {

           
            string XmlFilePath = Application.StartupPath   "\\BackFilePath.xml";

            if (System.IO.File.Exists(XmlFilePath))
            {

                //创建XML实例
                XmlDocument xmlDoc = new XmlDocument();
                //加载XML文件
                xmlDoc.Load(XmlFilePath);

                //设置要查询的节点
                string XmlPathNode = "//ConfigInfo/AutoStart";
                //获取查询节点的属性值
                if (xmlDoc.SelectSingleNode(XmlPathNode).Attributes["State"].Value.ToString() == "True")
                {
                    
                    AutoStart.Checked = true;

                    Text_SavePath.Text = xmlDoc.SelectSingleNode("//ConfigInfo/BackFilePath").Attributes["Path"].Value.ToString();
                    Text_ServerIP.Text = xmlDoc.SelectSingleNode("//ConfigInfo/ServerIP").Attributes["IP"].Value.ToString();
                    Text_ServerPort.Value = Convert.ToInt32(xmlDoc.SelectSingleNode("//ConfigInfo/ServerPort").Attributes["Port"].Value);


                    if (xmlDoc.SelectSingleNode("//ConfigInfo/SaveAllFileAndDelfile").Attributes["SaveState"].Value.ToString() == "True")
                    {
                        Check_SaveFileAS.Checked = true;
                    }
                    else
                    {
                        Check_SaveFileAS.Checked = false;
                    }
                    
                    Text_DelFileTime.Value = Convert.ToInt32(xmlDoc.SelectSingleNode("//ConfigInfo/SaveDayFile").Attributes["Days"].Value);
                    DelFileTime.Value = Convert.ToInt32(xmlDoc.SelectSingleNode("//ConfigInfo/DelFileTime").Attributes["Time"].Value);

                    StartListentServer();
                    
                }
                else
                {
                   
                    AutoStart.Checked = false;
                }

               
            }


            IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());

            Text_ServerIP.Text = ipe.AddressList[0].ToString();

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.Visible = false;
            ShowInTaskbar = true;
        }

        private void frm_NetServer_Main_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                notifyIcon1.Visible = true;
            }
        }

       


        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore.exe", "http://blog.csdn.net/psuaije"); 
            

        }

        /// <summary>
        /// 开始删除过期文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取计算机当前时间
            int intHour = DateTime.Now.Hour;
            int intMinute = DateTime.Now.Minute;
            int intSecond = DateTime.Now.Second;
           // MessageBox.Show(intHour.ToString()   " "   intMinute.ToString()   " "   intSecond.ToString());

            if (intHour == Convert.ToInt32(DelFileTime.Value) && intMinute == 0 && intSecond == 1 && Check_SaveFileAS.Checked == true)
            {
                
                    ListBox_LogMsg.Items.Add(System.DateTime.Now.ToString()   ":开始删除过期文件...");

                    System.IO.DirectoryInfo aDirFile = new System.IO.DirectoryInfo(Text_SavePath.Text);
                    System.IO.FileInfo[] aFileList = aDirFile.GetFiles();
                    for (int i = 0; i < aFileList.Length; i  )
                    {
                        string aFIle = aFileList[i].FullName;
                        if (aFIle.Substring(aFIle.LastIndexOf(".")).ToString().ToLower() != ".SDM")
                        {
                            System.IO.FileInfo file = new System.IO.FileInfo(aFIle);

                            if (Convert.ToInt32(file.CreationTime.ToString("yyyy-MM-dd").Replace("-", "")) < Convert.ToInt32(System.DateTime.Now.AddDays(-1 * Convert.ToInt32(Text_DelFileTime.Value)).ToString("yyyy-MM-dd").Replace("-", "")))
                            {
                                System.IO.File.Delete(aFIle);
                                ListBox_LogMsg.Items.Add("过期文件["   aFIle   "]文件删除成功.");
                            }
                        }
                        else
                        {
                            ListBox_LogMsg.Items.Add("文件"   aFIle   "为传输临时文件,不要删除,如果需除请手动删除。");
                        }

                    }
              

                


            }
        }

        private void 帮助HToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frm_Help _frm_Help = new frm_Help();
            _frm_Help.Show();
        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 设置自动启动程序和备份文件参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutoStart_Click(object sender, EventArgs e)
        {
            if (AutoStart.Checked)
            {
                if (Text_SavePath.Text != "" && Text_ServerIP.Text != "" && Text_ServerPort.Value > 0)
                {
                    if (MessageBox.Show("是否启用开机自动启动程序,并自动监听端口?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {

                        try
                        {
                            //获取程序的完整路径
                            string dir = System.Windows.Forms.Application.ExecutablePath;
                            //设置开启启动
                            RegistryKey akey = Registry.LocalMachine;
                            akey = akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run", true);
                            akey.SetValue("NetServer", dir);
                            akey.Close();
                            //设置开机启动参数
                            XmlDocument doc = new XmlDocument();
                            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null); doc.AppendChild(dec);  //创建一个根节点(一级)  

                            XmlElement root = doc.CreateElement("ConfigInfo");
                            doc.AppendChild(root);

                            //自动启动状态
                            XmlElement AuotStart = doc.CreateElement("AutoStart");
                            AuotStart.SetAttribute("State", "True");
                            root.AppendChild(AuotStart);

                            //存储备份文件保存路径
                            XmlElement BackSavePath = doc.CreateElement("BackFilePath");
                            BackSavePath.SetAttribute("Path", Text_SavePath.Text.Trim().ToString());
                            root.AppendChild(BackSavePath);

                            //存储服务端IP
                            XmlElement ServerIP = doc.CreateElement("ServerIP");
                            ServerIP.SetAttribute("IP", Text_ServerIP.Text);
                            root.AppendChild(ServerIP);

                            //存储监听端口
                            XmlElement ServerPort = doc.CreateElement("ServerPort");
                            ServerPort.SetAttribute("Port", Text_ServerPort.Value.ToString());
                            root.AppendChild(ServerPort);

                            //是否按时间保存文件,并删除过期文件
                            XmlElement DateTimeSaveFileAndDelFile = doc.CreateElement("SaveAllFileAndDelfile");
                            DateTimeSaveFileAndDelFile.SetAttribute("SaveState", Check_SaveFileAS.Checked.ToString());
                            root.AppendChild(DateTimeSaveFileAndDelFile);

                            //保存几天之前的文件
                            XmlElement SaveDayFile = doc.CreateElement("SaveDayFile");
                            SaveDayFile.SetAttribute("Days", Text_DelFileTime.Value.ToString());
                            root.AppendChild(SaveDayFile);

                            //删除过期文件时间
                            XmlElement DelFileDateTime = doc.CreateElement("DelFileTime");
                            DelFileDateTime.SetAttribute("Time", DelFileTime.Value.ToString());
                            root.AppendChild(DelFileDateTime);

                            //保存配置文件
                            doc.Save(Application.StartupPath   "\\BackFilePath.xml");
                            AutoStart.Checked = true;

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                }
                else
                {
                    AutoStart.Checked = false;

                    MessageBox.Show("请查看文件保存路径及其他参数设置是否为空!");
                }

            }
            else
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null); doc.AppendChild(dec);  //创建一个根节点(一级)  

                    XmlElement root = doc.CreateElement("ConfigInfo");
                    doc.AppendChild(root);

                    //自动启动状态
                    XmlElement AuotStart = doc.CreateElement("AutoStart");
                    AuotStart.SetAttribute("State", "false");
                    root.AppendChild(AuotStart);
                    //保存配置文件
                    doc.Save(Application.StartupPath   "\\BackFilePath.xml");
                    AutoStart.Checked = true;


                    RegistryKey akey = Registry.LocalMachine;
                    akey = akey.OpenSubKey(@"SOFTWARE\Microsoft\windows\CurrentVersion\Run", true);
                    akey.DeleteValue("NetServer", false);
                    akey.Close();
                    AutoStart.Checked = false;
                    MessageBox.Show("随机启动功能已经关闭!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

        }

        private void Check_SaveFileAS_Click(object sender, EventArgs e)
        {
            if (Check_SaveFileAS.Checked)
            {

                if (MessageBox.Show("开启按时间另存功能后,将自动开启清理过期文件!\n注意:将会删除文件夹内的所有过期文件!是否启动?", "自动清理提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Check_SaveFileAS.Checked = true;
                    Text_DelFileTime.Enabled = true;
                    DelFileTime.Enabled = true;
                }
                else
                {
                    Check_SaveFileAS.Checked = false;


                }

            }
            else
            {
                Text_DelFileTime.Enabled = false;
                DelFileTime.Enabled = false;
            }
        }

    
    }
}


实例下载地址

局域网文件定时传输V1.2版源代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警