隐藏

c# 后台监控测试数据

发布:2019/11/12 11:04:43作者:管理员 来源:本站 浏览次数:1045

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;


namespace MoveTestData_V1._00
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        string Sourcepath = string.Empty;//源路径
        string TargetPath = string.Empty;//目标路径
        public void ReadCfg(string CfgName)
        {
            FileStream fs = new FileStream(CfgName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            string[] Key = { "SourcesPath", "TargetPath" };
            try
            {
                string Temp = string.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    string[] Array = Temp.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    if (Array[0].Trim() == Key[0].Trim()) Sourcepath = Array[1].Trim();
                    else if (Array[0].Trim() == Key[1].Trim()) TargetPath = Array[1].Trim();
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("配置读取错误:" + ex.Message);
            }
            finally
            {
                sr.Close();
                fs.Close();
            }
        }


        public static bool connectShareDoc(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 + " /User:" + userName + " " + passWord;
                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
                {
                    MessageBox.Show("网络连接错误:" + errormsg);
                    Flag = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("网络连接异常:" + ex.Message);
                Flag = false;
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }


        public bool DisposeConnect()//断开本地网络连接
        {
            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 * /d /y";
                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
                {
                    Flag = false;
                    MessageBox.Show(errormsg);
                }
            }
            catch (Exception ex)
            {
                Flag = false;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                proc.Close();
                proc.Dispose();
            }
            return Flag;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName("MoveTestData V1.00");
            if(myProcesses.Length>1)
            {
                MessageBox.Show("程式已启动!!");
                Application.Exit();
            }
            ReadCfg("config.ini");//读取配置文件
            if (connectShareDoc(@"\\Server1\data\LOG\XPLOG", "admin", "test") == false)
            {
                DisposeConnect();//断开网络
                MessageBox.Show("无法连接服务器,程式结束网络后自动退出!!");
                Application.Exit();
            }
            timer1.Enabled = true;
        }


        public void GetDirectoryTextFileName(string DirectoryPath)
        {
            try
            {
                DirectoryInfo dinfo=new DirectoryInfo(DirectoryPath);//实例化DirectoryInfo
                FileSystemInfo[] fsinfos = dinfo.GetFileSystemInfos();
                foreach(FileSystemInfo fsinfo in fsinfos)
                {
                    if(fsinfo is FileInfo)
                    {
                        if (Path.GetExtension(fsinfo.Name) == @".txt")
                            GetTextFileName.Add(fsinfo.Name.Trim());
                    }
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("目录读取错误:"+ex.Message);
            }
        }


        public void FillMoveTestData(string SourcesPathName,string TargetPathName)
        {
            List<string> temp = new List<string>();
            FileStream fs = new FileStream(SourcesPathName,FileMode.Open,FileAccess.Read);
            StreamReader sr=new StreamReader(fs);
            try
            {
                string Temp = string.Empty;
                while ((Temp = sr.ReadLine()) != null)
                {
                    temp.Add(Temp);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sr.Close();
                fs.Close();
                File.Delete(SourcesPathName);
            }
            FileStream fs1;
            if(File.Exists(TargetPathName))
            {
                fs1 = new FileStream(TargetPathName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs1 = new FileStream(TargetPathName,FileMode.Create,FileAccess.Write);
            }
            StreamWriter swMyfile = new StreamWriter(fs1);
            foreach(string str in temp)
            {
                swMyfile.WriteLine(str);
            }
            swMyfile.Close();
            fs.Close();
        }


        List<string> GetTextFileName = new List<string>();//初始化
        private void timer1_Tick(object sender, EventArgs e)
        {
            GetTextFileName.RemoveRange(0,GetTextFileName.Count);
            GetDirectoryTextFileName(Sourcepath);//读取源路径测试数据文本名称
            if (GetTextFileName.Count>=1)
            {
                foreach (string str in GetTextFileName)
                {
                    FillMoveTestData(Sourcepath + @"\" + str, TargetPath+@"\"+str);
                }
            }
            
        }


        private void Form1_Shown(object sender, EventArgs e)
        {
            this.BeginInvoke(new Action(() =>
                {
                    this.Hide();
                    this.Opacity = 1;
                }));
        }
    }
}