基于C#+WINFORM的进账平台开发(C#文件处理1)

*基于C#+WINFORM的进账平台开发(C#文件处理1)


前言

公司生产需要开发一个工站进账系统,第一次接触C#架构开发,积累项目经验。本文实现了基于C#Winform架构的程序开发


一、程序界面

在这里插入图片描述用户通过Input区域输入测试信息,程序在后台根据输入信息来匹配和抓取后台测试数据并将进账所需要的测试信息在OUTPUT窗口实时显示。

二、代码实现

1.主窗体设计代码

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

    public partial class JustOne_AutoLogin : Skin_VS
    {
        //变量初始化
        private static string Configinipath = Environment.CurrentDirectory.ToString() + @"\Config.ini";
        string SN = "";
        string CM = "";
        string Test_status = "";
        string Tstation = "";
        string DataPath = "";
        string InfoPath = "";
        string XmlPath = "";
        string OutPath = "";
        string uut = "";
        string project = "";
        //string Tstation = "";
        FtpClient Justone_Ftp;
        Thread th;
        int m = 0;
        public JustOne_AutoLogin()
        {
            InitializeComponent();
        }

        //窗体初始化函数
        private void JustOne_AutoLogin_Load(object sender, EventArgs e)
        {
            Justone_Ftp = new FtpClient(Operini.ReadIni("FTP", "FTPHost", "0", Configinipath), Operini.ReadIni("FTP", "User", "0", Configinipath), Operini.ReadIni("FTP", "PassWord", "0", Configinipath));
            Control.CheckForIllegalCrossThreadCalls = false;
            this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
            notifyIcon1.Icon = new Icon("BLUE.ICO");
            notifyIcon1.Visible = false;
            notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);

        }

        //绑定Tstation测试工站信息
        private void TStation_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Tstation = TStation.SelectedItem.ToString();
            richTextBox1.AppendText("[*]Test Station:" + Tstation + "\r\n");
            Project.Focus();
        }

        //绑定机台号及UUT数量
        private void Project_SelectionChangeCommitted(object sender, EventArgs e)
        {
            project = Project.SelectedItem.ToString();
            richTextBox1.AppendText("[*]Project:" + project + "\r\n");
            switch (project)
            {
                case "Alterego":
                    m = 4;
                    break;
                case "Ancho":
                    m = 4;
                    break;
                case "Beach Comber":
                    m = 1;
                    break;
                case "Cayenee":
                    m = 4;
                    break;
                case "Luna":
                    m = 2;
                    break;
                case "Oberon":
                    m = 2;
                    break;
            }
            OPID.Focus();
        }

        //绑定操作员工号
        private void OPID_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyValue == 13)   // 判断是否按 ENTER 键
            {
                OPID.Text = "GEMOPS";
                richTextBox1.AppendText("[*]Operator ID:" + OPID.Text + "\r\n");
                PN_text.Focus();
            }
            else
            {
                MessageBox.Show("Operator ID is invalued!!!", this.Text);
                OPID.Clear();
            }
        }

        //绑定PN
        private void PN_text_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyValue == 13)   // 判断是否按 ENTER 键
            {
                if ((PN_text.Text.Length < Convert.ToInt16(Operini.ReadIni("set", "PN_Length_Min", "0", Configinipath))) || (PN_text.Text.Length > Convert.ToInt16(Operini.ReadIni("set", "PN_Length_Max", "0", Configinipath))))
                {
                    MessageBox.Show("PN is invalued!!!", this.Text);
                    PN_text.Clear();
                }
                else
                {
                    PN_text.Text = PN_text.Text.ToUpper();
                    richTextBox1.AppendText("[*]PN:" + PN_text.Text + "\r\n");
                    richTextBox1.AppendText("[*]Scan Date:");
                    richTextBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                    richTextBox1.AppendText("[*]Please start testing!\r\n");
                    PN_text.Focus();
                }

            }

        }

        //Start函数
        private void button_A_Click(object sender, EventArgs e)
        {
            th = new Thread(cycle);
            th.IsBackground = true;
            th.Start();
        }

        //开启线程循环
        private void cycle()
        {
            while (true)
            {
                for (int i = 0; i < m; i++)
                {
                    uut = string.Format("UUT{0}", i + 1);
                    DataPath = Operini.ReadIni("Infopath", "Infopath", "0", Configinipath) + uut + "\\";
                    Read_Info();
                    Write_Info();
                    Thread.Sleep(15000);
                    richTextBox1.Clear();
                    richTextBox2.Clear();
                }
            }

        }

        //从相关文件中获取信息
        private void Read_Info()
        {
            string path = InfoAbstract.File_List(DataPath);
            richTextBox1.AppendText("[*]" + path + "\r\n");
            if (path.Contains(Operini.ReadIni("set", "KeyWord", "0", Configinipath)))
            {
                string PN = PN_text.Text.Replace("\n", "").Replace("\r", "").Replace("\t", "");
                string CMSN = InfoAbstract.File_Find2(path, Operini.ReadIni("set", "KeyWord", "0", Configinipath), Operini.ReadIni("set", "KeyWord1", "0", Configinipath), PN);
                if (CMSN != "000")
                {
                    CM = CMSN.Split(':')[0];
                    SN = CMSN.Split(':')[1];
                    InfoPath = DataPath + "sequencerinfo.log";
                    Test_status = InfoAbstract.Info_File(InfoPath, DataPath, (Operini.ReadIni("set", "TestStep", "0", Configinipath)));
                }
                else
                {
                    richTextBox1.AppendText("[*]No Testing information to abstrated!" + "\r\n");
                }
            }
            else
            {
                XmlPath = DataPath + "datacollect.xml";
                CM = Operini.ReadIni("set", "Station_ID", "0", Configinipath);
                SN = InfoAbstract.Find_File(XmlPath);
                int step = 0;
                int.TryParse(Operini.ReadIni("set", "TestStep", "0", Configinipath), out step);
                //richTextBox1.AppendText(step.ToString());
                InfoPath = DataPath + "sequencerinfo.log";
                Test_status = InfoAbstract.Info_File_CIT(InfoPath, DataPath, step);
            }
            richTextBox1.AppendText("[*]" + Test_status + "\r\n");
            richTextBox2.AppendText("Log Source:" + DataPath + "\r\n");
            richTextBox2.AppendText("Log Source:" + InfoPath + "\r\n");
        }

        //将测试信息写入目标文件后传到FTP
        private void Write_Info()
        {
            OutPath = DataPath + "Test.log";
            string Station_ID = CM.ToUpper() ;
            string SN_ID = SN.ToUpper() ;
            string EPM = OPID.Text ;
            string UUT = uut.Split('T')[1];
            string SLOT = Operini.ReadIni("set", "SLOT", "0", Configinipath);
            string STATUS = "";
            string ERRORCODE = "";
            string PN = PN_text.Text;
            string Program = Operini.ReadIni("set", "Program", "0", Configinipath);
            string Datacode = DateTime.Now.ToString("yyyyMMdd");

            ///文件路径
            string PassPath = Operini.ReadIni("Outpath", "passpath", "0", Configinipath);
            string FailPath = Operini.ReadIni("Outpath", "failpath", "0", Configinipath);
            string Ftpsrc = PassPath + SN_ID + "_temp.log";
            string dpath = FailPath + SN_ID + "_" + "temp.log";
            string NetPath = Operini.ReadIni("Outpath", "Netpath", "0", Configinipath);
            //string AttPath = DataPath + PN + "_" + SN_ID + "_" + Test_status + "_" + Datacode + ".log";
            //string AttPathPass = PassPath + PN + "_" + SN_ID + "_" + Test_status + "_" + Datacode + ".log";
            //string AttPathFail = FailPath + PN + "_" + SN_ID + "_" + Test_status + "_" + Datacode + ".log";


            if (Test_status.Contains("PASS"))
            {
                STATUS = Test_status;
                string AttPath = DataPath + Program + "_" + Tstation + "_" + PN + "_" + SN_ID + "_" + STATUS + "_" + Datacode + ".log";
                string AttPathPass = PassPath + Program + "_" + Tstation + "_" + PN + "_" + SN_ID + "_" + STATUS + "_" + Datacode + ".log";
                ERRORCODE = "0";
                if (SN != "000")
                {
                    InfoAbstract.WriteLineToFile(Station_ID, OutPath);
                    InfoAbstract.WriteLineToFile(SN_ID, OutPath);
                    InfoAbstract.WriteLineToFile(EPM, OutPath);
                    InfoAbstract.WriteLineToFile(UUT, OutPath);
                    InfoAbstract.WriteLineToFile(SLOT, OutPath);
                    InfoAbstract.WriteLineToFile(STATUS, OutPath);
                    InfoAbstract.WriteLineToFile(ERRORCODE, OutPath);

                    InfoAbstract.WriteLineToFile("PRODUCT_NAME:"+ project, AttPath);
                    InfoAbstract.WriteLineToFile("STATION:" + Tstation, AttPath);
                    InfoAbstract.WriteLineToFile("DIAGS_PROGRAM:", AttPath);
                    InfoAbstract.WriteLineToFile("DIAGS_VER:V1.0",AttPath);
                    InfoAbstract.WriteLineToFile("PN:" + PN, AttPath);
                    InfoAbstract.WriteLineToFile("FIXID:" + Station_ID, AttPath);
                    InfoAbstract.WriteLineToFile("SN:" + SN_ID, AttPath);
                    InfoAbstract.WriteLineToFile("OPID:" + EPM, AttPath);
                    InfoAbstract.WriteLineToFile("UUT:" + UUT, AttPath);
                    InfoAbstract.WriteLineToFile("SLOT:" + SLOT, AttPath);
                    InfoAbstract.WriteLineToFile("TESTRESULT:" + STATUS, AttPath);
                    InfoAbstract.WriteLineToFile("FAILNOTE:" + ERRORCODE, AttPath);
                    string FTPDST = PassPath + "PASSRETURN.log";
                    //string Ftpsrc = "C:\\Users\\Administrator\\Desktop\\sequencerinfo.log";
                    richTextBox1.AppendText("[*]" + InfoAbstract.File_Move(OutPath, PassPath, SN_ID) + "\r\n");
                    richTextBox1.AppendText("[*]" + InfoAbstract.File_Move_Att(AttPath, PassPath,Program,Tstation, PN, SN_ID, Test_status, Datacode) + "\r\n");
                    richTextBox1.AppendText("[*]" + Ftpsrc + "\r\n");
                    //File.Delete(OutPath);
                    //NET
                    richTextBox1.AppendText("[*]" + InfoAbstract.File_To_Net(Ftpsrc, NetPath, SN_ID) + "\r\n");
                    richTextBox1.AppendText("[*]" + InfoAbstract.ATTFileToNet(AttPathPass, NetPath, Program,Tstation, PN, SN_ID, Test_status, Datacode) + "\r\n");
                    //richTextBox1.AppendText("[*]" + Justone_Ftp.get(FTPFROMAT, FTPDST) + "\r\n");

                    string sLine = File.ReadAllText(PassPath + DateTime.Now.ToString("yyyyMMdd") + ".log");
                    if (sLine.Contains(SN_ID))
                    {
                        richTextBox1.AppendText("[*].......");
                        File.Delete(dpath);
                        File.Delete(AttPathPass);
                    }
                    File.Delete(OutPath);
                    //File.Delete(AttPath);
                    //File.Delete(Ftpsrc);
                }
                else 
                {
                    richTextBox1.AppendText("[*]No Testing information to abstrated!" + "\r\n");
                }
                
            }
            else if (Test_status.Contains("WAIT"))
            {
                richTextBox1.AppendText("Waiting For PASS/FAIL");
            }
            else
            {
                if (SN != "000")
                {
                    
                    STATUS = "FAIL";
                    ERRORCODE = Test_status;
                    string AttPath = DataPath + Program + "_" + Tstation + "_" + PN + "_" + SN_ID + "_" + STATUS + "_" + Datacode + ".log";
                    string AttPathFail = FailPath + Program + "_" + Tstation + "_" + PN + "_" + SN_ID + "_" + STATUS + "_" + Datacode + ".log";
                    InfoAbstract.WriteLineToFile(Station_ID, OutPath);
                    InfoAbstract.WriteLineToFile(SN_ID, OutPath);
                    InfoAbstract.WriteLineToFile(EPM, OutPath);
                    InfoAbstract.WriteLineToFile(UUT, OutPath);
                    InfoAbstract.WriteLineToFile(SLOT, OutPath);
                    InfoAbstract.WriteLineToFile(STATUS, OutPath);
                    InfoAbstract.WriteLineToFile(ERRORCODE, OutPath);

                    InfoAbstract.WriteLineToFile("PRODUCT_NAME:" + project, AttPath);
                    InfoAbstract.WriteLineToFile("STATION:" + Tstation, AttPath);
                    InfoAbstract.WriteLineToFile("DIAGS_PROGRAM:", AttPath);
                    InfoAbstract.WriteLineToFile("DIAGS_VER:V1.0", AttPath);
                    InfoAbstract.WriteLineToFile("PN:" + PN, AttPath);
                    InfoAbstract.WriteLineToFile("FIXID:" + Station_ID, AttPath);
                    InfoAbstract.WriteLineToFile("SN:" + SN_ID, AttPath);
                    InfoAbstract.WriteLineToFile("OPID:" + EPM, AttPath);
                    InfoAbstract.WriteLineToFile("UUT:" + UUT, AttPath);
                    InfoAbstract.WriteLineToFile("SLOT:" + SLOT, AttPath);
                    InfoAbstract.WriteLineToFile("TESTRESULT:" + STATUS, AttPath);
                    InfoAbstract.WriteLineToFile("FAILNOTE:" + ERRORCODE, AttPath);
                    string FTPDST = FailPath + "FAILRETURN.log";
                    richTextBox1.AppendText("[*]" + InfoAbstract.File_Move_Fail(OutPath, FailPath, SN_ID) + "\r\n");
                    richTextBox1.AppendText("[*]" + InfoAbstract.File_Move_Att(AttPath, FailPath, Program,Tstation, PN, SN_ID, STATUS, Datacode) + "\r\n");
                    if (File.Exists(PassPath + DateTime.Now.ToString("yyyyMMdd") + ".log"))
                    {
                        string sLine = File.ReadAllText(PassPath + DateTime.Now.ToString("yyyyMMdd") + ".log");
                        //string dpath = FailPath + SN + "_" + "temp.log";
                        if (sLine.Contains(SN_ID))
                        {
                            richTextBox1.AppendText("[*].......");
                            File.Delete(dpath);
                            File.Delete(AttPathFail);
                            richTextBox1.AppendText("[*]THIS PIECE OF SN PASSED!\r\n");
                        }
                        else
                        {
                            richTextBox1.AppendText("[*]" + InfoAbstract.ATTFileToNet(AttPathFail, NetPath, Program,Tstation, PN, SN_ID, STATUS, Datacode) + "\r\n");
                            //richTextBox1.AppendText("[*]" + InfoAbstract.File_To_Net(dpath, NetPath,SN) + "\r\n");
                            richTextBox1.AppendText("[*]THIS PIECE OF SN FAILED!\r\n");
                        }
                    }
                    
                    File.Delete(OutPath);
                    File.Delete(AttPath);
                }
                else
                {
                    richTextBox1.AppendText("[*]No Testing information to abstrated!" + "\r\n");
                }
            }
        }

        //结束按键
        private void button_B_Click(object sender, EventArgs e)
        {
            if (th != null && th.IsAlive)
            {
                th.Abort();
            }
            richTextBox1.Clear();
            richTextBox2.Clear();
            OPID.Clear();
            PN_text.Clear();
        }

        //最小化到系统托盘
        private void JustOne_AutoLogin_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = false;
                this.notifyIcon1.Visible = true;
                this.notifyIcon1.ShowBalloonTip(3, "提示", "系统仍在运行!", ToolTipIcon.Info);
            }
        }
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Activate();
            this.Visible = true;
            this.notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;

        }

2.文件操作涉及到的子函数

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Threading.Tasks;
using System.Xml;

//自定义Log信息提取子类

namespace JustOne_AutoLogin

{
    class InfoAbstract
    {
        //读.xml文件SN、PN、PPID
        public static string Find_File(string filepath)
        {
            string cont = "";
            try
            {
                if (!File.Exists(filepath))
                {
                    cont = cont + ("Err: " + filepath + " not find");
                    return "000";
                }
                DataSet thisDataSet = new DataSet();
                thisDataSet.ReadXml(@filepath);//读Xml文件
                foreach (DataRow custRow in thisDataSet.Tables["LoginData"].Rows)
                {
                    string[] TLA = { custRow["DAE0_TLA_SN"].ToString(), custRow["DAE0_TLA_PN"].ToString() };//取TLA SN和PN
                        //cont = cont + "SN:" + TLA[0] + "\r\n" + "PN:" + TLA[1] + "\r\n" + "PPID:" + TLA[2] + "\r\n";
                    cont = cont + TLA[0];
                }
                return cont;
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }

        }

        //读BFT.log文件Computer Name和SN
        public static string File_Find2(string filepath, string Keyword, string Keyword1, string Keyword2)
        {
            string cont = "";
            try
            {
                if (!File.Exists(filepath))
                {
                    cont = "000";
                    return cont;
                }
                else
                {
                    using (StreamReader sr = new StreamReader(filepath))
                    {
                        string strLine;
                        while ((strLine = sr.ReadLine()) != null)
                        {
                            if (strLine.Contains(Keyword1))
                            {
                                cont = strLine.Split(':')[1];//取Computer Name
                            }
                            if (strLine.Contains(Keyword2))
                            {
                                cont = cont + ":" + strLine.Split(new char[] { ',', ':' }, 10)[7];//取SN
                            }
                        }
                    }
                    return cont;
                }
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }
        //INFO 测试结果读取
        public static string Info_File(string filepath, string datapath, string step)
        {
            string cont = "";
            try
            {
                //string temppath = datapath + "temp.log";
                if (!File.Exists(filepath))
                {
                    cont = cont + ("Err: " + filepath + " not find");
                    return cont;
                }
                else
                {
                    string[] ALLlines = File.ReadAllLines(filepath);
                    //int count = 0; 
                    string search = string.Format("Test {0}", step);
                    string search_Fail = "Fail received on TestStep";
                    //string[] ALL = File.ReadAllLines(temppath);
                    string failstep = "";
                    string failinfo = "";
                    foreach (string item in ALLlines)
                    {
                        cont = item;
                        if (item.Contains(search))
                        {
                            cont = "PASS";
                            return cont;
                        }
                        else if (item.Contains(search_Fail))
                        {
                            failstep = item.Substring(36, 11);
                            failinfo = string.Format("Fail received on {0}", failstep);
                            cont = failinfo;
                            return cont;
                        }
                        else
                        {
                            cont = "WAIT";
                        }
                    }
                    return cont;
                }
               
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }

        //针对CIT类型Fail不中断的机台FileInfo信息读取
        public static string Info_File_CIT(string filepath, string datapath, int step)
        {
            string cont = "";
            //string temppath = datapath + "temp.log";
            try
            {
                if (!File.Exists(filepath))
                {
                    cont = cont + ("Err: " + filepath + " not find");
                    return cont;
                }
                else
                {
                    string search = "Pass received on TestStep";
                    string search_Fail = "Received error message";
                    string LastFail = "";
                    string[] ALLlines = File.ReadAllLines(filepath);
                    List<string> PASSLIST = new List<string>();
                    List<string> FAILLIST = new List<string>();
                    foreach (string item in ALLlines)
                    {
                        if (item.Contains("Module[0]") && item.Contains(search_Fail))
                        {
                            int p = 0;
                            p = Array.IndexOf(ALLlines, item);
                            string i = ALLlines[p - 1];
                            string j = ALLlines[p + 1];
                            if (i.Contains("Module[0]:"))
                            {
                                string[] TempList = i.Split(' ');
                                FAILLIST.Add(TempList[13]);
                                LastFail = TempList[13];
                            }
                            else if (j.Contains("Module[0]:"))
                            {
                                string[] TempList = j.Split(' ');
                                FAILLIST.Add(TempList[13]);
                                LastFail = TempList[13];
                            }
                        }
                        if (item.Contains("Module[0]") && item.Contains("Abort"))
                        {
                            string[] TempList = item.Split(' ');
                            FAILLIST.Add(TempList[13]);
                            LastFail = TempList[13];
                        }
                        if (item.Contains("Module[0]") && item.Contains(search))
                        {
                            string[] TempList = item.Split(' ');
                            PASSLIST.Add(TempList[13]);
                        }

                    }
                    HashSet<string> Fail = new HashSet<string>(FAILLIST);
                    int m = Fail.Count;
                    Fail.ToList().ForEach(i => Console.WriteLine(i));
                    HashSet<string> Pass = new HashSet<string>(PASSLIST);
                    Pass.ToList().ForEach(i => Console.WriteLine(i));
                    if (Pass.Count == step)
                    {
                        cont = "PASS";
                        // Console.WriteLine(cont);
                    }
                    else if (LastFail == " ")
                    {
                        cont = "WAIT";
                    }
                    else
                    {
                        cont = string.Format("Fail received on TestStep{0}", LastFail);
                    }
                    //File.Delete(temppath);
                    return cont;
                }
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }

        //在文件夹下按关键字筛选文件
        public static string File_List(string filepath)
        {
            string cont = "";
            try
            {
                string[] floder = Directory.GetFiles(filepath);
                    foreach (string file in floder)
                    {
                        if (file.Contains("BFT"))
                        {
                            cont = cont + file;
                        }
                        
                    }
                return cont;
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }

        //字符串追加写入文件
        public static string WriteLineToFile(string line, string filepath)
        {
            try
            {
                string cont = "";
                FileStream fs;
                StreamWriter sw;
                if (File.Exists(filepath))//判断目标路径是已有在目标文件
                {

                    StreamReader objReader = new StreamReader(filepath);
                    string sLine;
                    while ((sLine = objReader.ReadLine()) != null)
                    {
                        cont = cont + sLine + '\n';
                    }
                    objReader.Close();
                    fs = new FileStream(filepath, FileMode.Append);//有就添加
                    sw = new StreamWriter(fs);//实例化 目标文件写DB1LOG 类
                    sw.WriteLine(line);//将此log写到目标文件
                    sw.Flush();//释放写类占用资源
                    fs.Close();//关闭写类
                    //}
                }
                else
                {
                    fs = new FileStream(filepath, FileMode.Create);//没有新建
                    sw = new StreamWriter(fs);//实例化 目标文件写DB1LOG 类
                    sw.WriteLine(line);//将此log写到目标文件
                    sw.Flush();//释放写类占用资源
                    fs.Close();//关闭写类
                }  
                return "OK";
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        //文件移动并去重
        public static string File_Move(string filepath, string temppath, string SN)
        {
            string cont = "";
            try
            {
                string datepath = temppath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                string dpath = temppath + SN+"_"+"temp.log";
                FileInfo FS = new FileInfo(filepath);
                FileInfo fd = new FileInfo(dpath);
                StreamWriter sw;
                if (File.Exists(datepath))//判断目标路径是已有在目标文件
                {
                    string sLine = File.ReadAllText(@datepath);
                    if (sLine.Contains(SN))
                    {
                        return cont = "Repeat Transfer!";
                    }
                    else
                    {
                        FileStream FILE = new FileStream(datepath, FileMode.Append);
                        using (sw = new StreamWriter(FILE))
                        {
                            sw.WriteLine(SN+" "+DateTime.Now.ToString("yyyyMMdd-hh:mm:ss"));
                            sw.Flush();
                        }
                        //File.Delete(dpath);
                        FS.CopyTo(dpath);
                    }
                }
                else
                {
                    FileStream FILE = new FileStream(datepath, FileMode.Create);
                    using (sw = new StreamWriter(FILE))
                    {
                        sw.WriteLine(SN + " " + DateTime.Now.ToString("yyyyMMdd-hh:mm:ss"));
                        sw.Flush();
                    }
                    //File.Delete(dpath);
                    FS.CopyTo(dpath);
                }
                FS.Delete();
                return cont = string.Format("{0} was copied to {1}", filepath, temppath);
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }
        //Fail文件移动并去重
        public static string File_Move_Fail(string filepath, string temppath, string SN)
        {
            string cont = "";
            try
            {
                string datepath = temppath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                string dpath = temppath + SN + "_" + "temp.log";
                FileInfo FS = new FileInfo(filepath);
                FileInfo fd = new FileInfo(dpath);
                StreamWriter sw;
                if (File.Exists(datepath))//判断目标路径是已有在目标文件
                {
                    string sLine = File.ReadAllText(@datepath);
                    if (sLine.Contains(SN))
                    {
                        File.Delete(dpath);
                        FS.CopyTo(dpath);
                    }
                    else
                    {
                        FileStream FILE = new FileStream(datepath, FileMode.Append);
                        using (sw = new StreamWriter(FILE))
                        {
                            sw.WriteLine(SN + " " + DateTime.Now.ToString("yyyyMMdd-hh:mm:ss"));
                            sw.Flush();
                        }
                        //File.Delete(dpath);
                        FS.CopyTo(dpath);
                    }
                }
                else
                {
                    FileStream FILE = new FileStream(datepath, FileMode.Create);
                    using (sw = new StreamWriter(FILE))
                    {
                        sw.WriteLine(SN + " " + DateTime.Now.ToString("yyyyMMdd-hh:mm:ss"));
                        sw.Flush();
                    }
                    //File.Delete(dpath);
                    FS.CopyTo(dpath);
                }
                FS.Delete();
                return cont = string.Format("{0} was copied to {1}", filepath, temppath);
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }

        public static string File_Move_Att(string filepath, string temppath, string PROGRAM,string Station,string PN, string SN, string Test_status, string Datacode)
        {
            string cont = "";
            try
            {
                string datepath = temppath + DateTime.Now.ToString("yyyyMMdd") + ".log";
                string dpath = temppath + PROGRAM + "_" + Station + "_" + PN + "_" + SN + "_" + Test_status + "_" + Datacode + ".log";
                FileInfo FS = new FileInfo(filepath);
                FileInfo fd = new FileInfo(dpath);
                //StreamWriter sw;
                if (File.Exists(dpath))//判断目标路径是已有在目标文件
                {
                   
                }
                else
                {
                    FS.CopyTo(dpath);
                }
                FS.Delete();
                return cont = string.Format("{0} was copied to {1}", filepath, temppath);
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }
        public static string File_To_Net(string temppath, string Netpath,string SN) 
        {
            string cont = "";
            string NetFile = Netpath+SN+".log";
            try
            {
                if (!File.Exists(temppath))
                {
                    cont = cont + ("Err: " + temppath + " not find");
                    return cont;
                }
                if (!File.Exists(NetFile))
                {
                    FileInfo FS = new FileInfo(temppath);
                    FS.CopyTo(NetFile);
                    cont = "SUCCESS TO NETFILE!";   
                }
                else
                {
                    cont = "REPASS TO NETFILE!";
                }
                return cont;
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }
        public static string ATTFileToNet(string temppath, string Netpath, string Program,string Station,string PN, string SN, string Test_status, string Datacode)
        {
            string cont = "";
            string NetFile = Netpath + Program + "_" + Station + "_" + PN + "_" + SN + "_" + Test_status + "_" + Datacode + ".log";
            try
            {
                if (!File.Exists(temppath))
                {
                    cont = cont + ("Err: " + temppath + " not find");
                    return cont;
                }
                if (!File.Exists(NetFile))
                {
                    FileInfo FS = new FileInfo(temppath);
                    FS.CopyTo(NetFile);
                    cont = "SUCCESS TO NETFILE!";
                }
                else
                {
                    cont = "REPASS TO NETFILE!";
                }
                return cont;
            }
            catch (Exception ex)
            {
                cont = cont + "Err:" + ex.Message;
                return cont;
            }
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值