2019-01-20

2019-01-20  本文已影响12人  鲁先森L

更新工具代码 进度1.0

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.VisualBasic.Devices;

namespace PMPPGEN
{
    public partial class Form1 : Form
    {
        #region "声明变量"

        //INI文件名
        private static string PMPPGEN_FileName = "PMPPGEN.ini";
        private static string PM7_FileName = "PM7.ini";
        private static string HDATE_FileName = "HDATE.ini";
        private static string VFREPORT_FileName = "VFREPORT.ini";
        //获取INI文件路径
        private string PMPPGEN_FilePath = Application.StartupPath + "\\" + PMPPGEN_FileName;
        private string PM7_FilePath = Application.StartupPath + "\\" + PM7_FileName;
        private string HDATE_FilePath = Application.StartupPath + "\\" + HDATE_FileName;
        private string VFREPORT_FilePath = Application.StartupPath + "\\" + VFREPORT_FileName;
        //元号固定用定义
        private string GENGO_Section = "cpGENGO";//PMPPGEN节点名
        private string PM7_Section = "cpGENGO";//PM7节点名
        private string JpnYears_Section = "JpnYears";//PM7文件中JpnYears节点
        private string WDATETIM_Section = "WDATETIM";//PM7文件中WDATETIM节点
        private string HDATE_Section = "NENGO";//HDATE节点名
        private string VFREPORT_Section = "Fclib";//VFREPORT节点名
        //HDATE的子节点
        private string gengo = "GENGO";
        private string ryaku = "RYAKU";
        private string startTime = "START";
        private string endTime = "END";
        private string nensa = "NENSA";
        //VFREPORT的子节点
        private string nengou = "Nengou";
        
        //错误窗口
        ErrorForm errorForm = new ErrorForm();
        //方法定义
        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="section">节点名称[如[TypeName]]</param>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        /// <param name="filepath">文件路径</param>
        /// <returns>??</returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">键</param>
        /// <param name="def">值</param>
        /// <param name="retval">strignbuilder对象</param>
        /// <param name="size">字节大小</param>
        /// <param name="filePath">文件路径</param>
        /// <returns>??</returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

        #endregion


        public Form1()
        {
            InitializeComponent();
            
        }
        
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
           //检查ini文件是否存在
            iniFileCheck();
        }

        /// <summary>
        /// 执行更新处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btGO_Click(object sender, EventArgs e)
        {
            //执行更新前先验证输入框
            inputCheck();
            //ini文件备份处理
            backUpFiles();
            //添加信息
            setPMPPGENInfo();
            //获取PMPPGEN节点信息
            setPM7Info();
            //更新HDATE.ini文件信息
            setHDATEInfo();
            //更新VFREPORT.ini文件信息
            setVFREPORTInfo();
            MessageBox.Show("ok!");
        }

        /// <summary>
        /// 画面关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 检查ini文件存在具体方法
        /// </summary>
        private void iniFileCheck() 
        {
            try
            {
                //判断PMPPGEN.INI文件是否存在
                if (!File.Exists(PMPPGEN_FilePath))
                {
                    MessageBox.Show("找不到环境文件'PMPPGEN.INI'!");
                    errorForm.ShowDialog();
                }

                //判断PM7.INI文件是否存在
                if (!File.Exists(PM7_FilePath))
                {
                    MessageBox.Show("找不到环境文件'PM7.INI'!");
                    errorForm.ShowDialog();
                }

                //判断HDATE.INI文件是否存在
                if (!File.Exists(HDATE_FilePath))
                {
                    MessageBox.Show("找不到环境文件'HDATE.INI'!");
                    errorForm.ShowDialog();
                }

                //判断VFREPORT.INI文件是否存在
                if (!File.Exists(VFREPORT_FilePath))
                {
                    MessageBox.Show("找不到环境文件'VFREPORT.INI'!");
                    errorForm.ShowDialog();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 画面输入验证
        /// </summary>
        private void inputCheck() 
        {
            try
            {
                if (Ed_GENGO.Text=="")
                {
                    MessageBox.Show("新元号が未入力です!");
                    Ed_GENGO.Focus();
                }else if (Ed_GKIGO.Text == "")
                {
                    MessageBox.Show("新元号記号が未入力です!");
                    Ed_GKIGO.Focus();
                }else if (Ed_GYY.Text == "")
                {
                    MessageBox.Show("元号開始日が未入力です!");
                    Ed_GYY.Focus();
                }else if (Ed_GDD.Text == "")
                {
                    MessageBox.Show("元号開始日が未入力です!");
                    Ed_GDD.Focus();
                }else if (Ed_GMM.Text == "")
                {
                    MessageBox.Show("元号開始日が未入力です!");
                    Ed_GMM.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 备份ini文件
        /// </summary>
        private void backUpFiles() 
        {
            try
            {
                //重命名ini文件并且备份到同一目录下         
                //string nowTime = Ed_GYY.Text + Ed_GMM.Text + Ed_GDD.Text;
                string newPM7_FileName = "「" + PM7_FileName.Substring(0, PM7_FileName.LastIndexOf('.')) + "_YYYYMMDD」";
                string newHDATE_FileName = "「" + HDATE_FileName.Substring(0, HDATE_FileName.LastIndexOf('.')) + "_YYYYMMDD」";
                string newVFREPORT_FileName = "「" + VFREPORT_FileName.Substring(0, VFREPORT_FileName.LastIndexOf('.')) + "_YYYYMMDD」";
                //备份文件已存在则删除
                if (File.Exists(Application.StartupPath + "\\" + newPM7_FileName))
                {
                    File.Delete(newPM7_FileName);
                }
                if (File.Exists(Application.StartupPath + "\\" + newHDATE_FileName))
                {
                    File.Delete(newHDATE_FileName);
                }
                if (File.Exists(Application.StartupPath + "\\" + newVFREPORT_FileName))
                {
                    File.Delete(newVFREPORT_FileName);
                }
                //文件备份
                File.Copy(PM7_FileName, newPM7_FileName);
                File.Copy(HDATE_FileName, newHDATE_FileName);
                File.Copy(VFREPORT_FileName, newVFREPORT_FileName);

            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("PM7"))
                {
                    MessageBox.Show("PM7.INIファイルをバックアップに失敗しました!");
                    errorForm.ShowDialog();
                }
                if (ex.Message.Contains("HDATE"))
                {
                    MessageBox.Show("HDATE.INIファイルをバックアップに失敗しました!");
                    errorForm.ShowDialog();
                }
                if (ex.Message.Contains("VFREPORT"))
                {
                    MessageBox.Show("VFREPORT.INIファイルをバックアップに失敗しました!");
                    errorForm.ShowDialog();
                }
            }
            
        }
        /// <summary>
        /// 设置PMPPGEN文件中节点信息
        /// </summary>
        private void setPMPPGENInfo()
        {
            //获取截至日期
            string newEndYY = ContentValue(GENGO_Section, "NewEndYY", PMPPGEN_FilePath);
            string newEndYMD = ContentValue(GENGO_Section, "NewEndYMD", PMPPGEN_FilePath);
            string newChkS = ContentValue(GENGO_Section, "NewChkS", PMPPGEN_FilePath);
            string newChkE = ContentValue(GENGO_Section, "NewChkE", PMPPGEN_FilePath);
            //执行更新的信息
            string newGENGO_Info = Ed_GYY.Text + "," + newEndYY + "," + Ed_GYY.Text + Ed_GMM.Text + Ed_GDD.Text + "," + newEndYMD + "," + Ed_GKIGO.Text.ToUpper() + "," + Ed_GKIGO.Text.ToLower() + "," + newChkS + "," + newChkE;
            try
            {
                WritePrivateProfileString(GENGO_Section, "NAME", ContentValue(GENGO_Section, "NAME", PMPPGEN_FilePath) + "," + Ed_GENGO.Text, PMPPGEN_FilePath);
                WritePrivateProfileString(GENGO_Section, Ed_GENGO.Text, newGENGO_Info, PMPPGEN_FilePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                //文件恢复处理
                fileRecovery();
                //错误提示
                errorForm.ShowDialog();
            }
        }
        /// <summary>
        /// 设置PM7文件节点信息
        /// </summary>
        private void setPM7Info() 
        {
            try
            {
                //获取PMPPGEN.ini文件中NAME节点的信息
                string pmppInfo = ContentValue(GENGO_Section,"NAME",PMPPGEN_FilePath);
                //根据NAME获取元号数组
                string[] gengoName = pmppInfo.Split(',');
                //具体元号对应的信息
                string[] infoArray = new string[gengoName.Length];

                //***   固定行更新   ****************************************************//
                WritePrivateProfileString(PM7_Section, "NAME", pmppInfo, PM7_FilePath);
                for (int i = 0; i < 3; i++)
                {
                    infoArray[i] = ContentValue(GENGO_Section, gengoName[i], PMPPGEN_FilePath);
                    //将PMPP的信息取出来给PM7各子节点
                    //***   [JpnYears]域  ***//
                    WritePrivateProfileString(JpnYears_Section, gengoName[i], infoArray[i].Split(',')[0], PM7_FilePath);
                    //***   [WDATETIM]域  ***//
                    WritePrivateProfileString(WDATETIM_Section, infoArray[i].Split(',')[0], infoArray[i].Split(',')[4] + gengoName[i], PM7_FilePath);
                    //***   [cpGENGO]域  ***//
                    WritePrivateProfileString(GENGO_Section, gengoName[i], infoArray[i], PM7_FilePath);
                }
                //[cpGENGO]域 第四行
                infoArray[3] = ContentValue(GENGO_Section, gengoName[3], PMPPGEN_FilePath);
                WritePrivateProfileString(GENGO_Section, gengoName[3], infoArray[3], PM7_FilePath);

                //***   最新行更新   ****************************************************//
                string newGengo = Ed_GENGO.Text;
                string newTime = Ed_GYY.Text+Ed_GMM.Text+Ed_GDD.Text;    
                //***   [JpnYears]域  ***//
                WritePrivateProfileString(JpnYears_Section, Ed_GENGO.Text,Ed_GYY.Text, PM7_FilePath);
                //***   [WDATETIM]域  ***//
                WritePrivateProfileString(WDATETIM_Section, Ed_GYY.Text, Ed_GKIGO.Text+Ed_GENGO.Text, PM7_FilePath);
                //***   [cpGENGO]域  ***//
                //获取截至日期
                string newEndYY = ContentValue(GENGO_Section, "NewEndYY", PMPPGEN_FilePath);
                string newEndYMD = ContentValue(GENGO_Section, "NewEndYMD", PMPPGEN_FilePath);
                string newChkS = ContentValue(GENGO_Section, "NewChkS", PMPPGEN_FilePath);
                string newChkE = ContentValue(GENGO_Section, "NewChkE", PMPPGEN_FilePath);
                string newInfo = Ed_GYY.Text + "," + newEndYY + "," + Ed_GYY.Text + Ed_GMM.Text + Ed_GDD.Text + "," + newEndYMD + "," + Ed_GKIGO.Text.ToUpper() + "," + Ed_GKIGO.Text.ToLower() + "," + newChkS + "," + newChkE;
                WritePrivateProfileString(GENGO_Section, Ed_GENGO.Text, newInfo, PM7_FilePath);
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("PM7.INIファイルを更新に失敗しました!");
                //文件恢复处理
                fileRecovery();
                //错误提示
                errorForm.ShowDialog();
            }
        }
        //根据节点对应key值获取value
        private string ContentValue(string section,string key,string filePath)
        {
            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(section, key, "", temp, 1024, filePath);
            return temp.ToString();
        }
        
        /// <summary>
        /// 设置HDATE文件中节点信息
        /// </summary>
        private void setHDATEInfo() 
        {
            try
            {
                //获取PMPPGEN文件信息
                string pmppInfo = ContentValue(GENGO_Section,"NAME",PMPPGEN_FilePath);
                string[] gengoName = pmppInfo.Split(',');
                int sectionSuffix = gengoName.Length;
                string[] infoArray = new string[sectionSuffix];
                //***   固定行更新   ****************************************************//
                for (int i = 0; i < 3; i++)
                {
                    infoArray[i] = ContentValue(GENGO_Section, gengoName[i], PMPPGEN_FilePath);
                    string[] newArray = infoArray[i].Split(',');
                    //开始年号
                    int startYear = Convert.ToInt16(newArray[2].Substring(0, 4)) - 1;
                    WritePrivateProfileString(HDATE_Section, gengo + (i + 1), gengoName[i], HDATE_FilePath);
                    WritePrivateProfileString(HDATE_Section, ryaku + (i + 1), newArray[4], HDATE_FilePath);
                    WritePrivateProfileString(HDATE_Section, startTime + (i + 1), newArray[2], HDATE_FilePath);
                    WritePrivateProfileString(HDATE_Section, endTime + (i + 1), newArray[3], HDATE_FilePath);
                    WritePrivateProfileString(HDATE_Section, nensa + (i + 1), startYear.ToString(), HDATE_FilePath);
                }
                //***   最新行更新   ****************************************************//
                infoArray[sectionSuffix - 1] = ContentValue(GENGO_Section, gengoName[sectionSuffix - 1], PMPPGEN_FilePath);
                string[] newArrayEnd = infoArray[sectionSuffix - 1].Split(',');
                int startYearEnd = Convert.ToInt16(Ed_GYY.Text) - 1;
                WritePrivateProfileString(HDATE_Section,gengo+sectionSuffix,Ed_GENGO.Text,HDATE_FilePath);
                WritePrivateProfileString(HDATE_Section, ryaku + sectionSuffix, Ed_GKIGO.Text, HDATE_FilePath);
                WritePrivateProfileString(HDATE_Section, startTime + sectionSuffix, Ed_GYY.Text+Ed_GMM.Text+Ed_GDD.Text, HDATE_FilePath);
                WritePrivateProfileString(HDATE_Section, endTime + sectionSuffix, newArrayEnd[3], HDATE_FilePath);
                WritePrivateProfileString(HDATE_Section, nensa + sectionSuffix, startYearEnd.ToString(), HDATE_FilePath);
            }
            catch (Exception)
            {
                //文件恢复处理
                fileRecovery();
                //错误提示
                errorForm.ShowDialog();
            }
        }
        /// <summary>
        /// 设置VFREPORT文件中节点信息
        /// </summary>
        private void setVFREPORTInfo() 
        {
            try
            {
                //获取PMPPGEN文件信息
                string pmppInfo = ContentValue(GENGO_Section, "NAME", PMPPGEN_FilePath);
                string[] gengoName = pmppInfo.Split(',');
                int sectionSuffix = gengoName.Length;
                string[] infoArray = new string[sectionSuffix];
                string nengouValue;
                //***   固定行更新   ****************************************************//
                for (int i = 0; i < 3; i++)
                {
                    infoArray[i] = ContentValue(GENGO_Section, gengoName[i], PMPPGEN_FilePath);
                    nengouValue =gengoName[i]+infoArray[i].Split(',')[4]+" "+infoArray[i].Split(',')[3];
                    WritePrivateProfileString(VFREPORT_Section, nengou + (i + 1), nengouValue, VFREPORT_FilePath);
                }
                //***   最新行更新   ****************************************************//
                string updateInfo = Ed_GENGO.Text + Ed_GKIGO.Text.ToUpper() + " " + Ed_GYY.Text + Ed_GMM.Text + Ed_GDD.Text;
                WritePrivateProfileString(VFREPORT_Section, nengou + sectionSuffix, updateInfo, VFREPORT_FilePath);

            }
            catch (Exception)
            {
                //文件恢复处理
                fileRecovery();
                //错误提示
                errorForm.ShowDialog();
            }
        }
        /// <summary>
        /// 文件恢复处理
        /// </summary>
        private void fileRecovery() 
        {
            try
            {
                //文件更新异常,首先删除文件
                File.Delete(PM7_FilePath);
                File.Delete(HDATE_FilePath);
                File.Delete(VFREPORT_FilePath);
                //重命名备份文件
                Computer MyComputer = new Computer();
                MyComputer.FileSystem.RenameFile("「PM7_YYYYMMDD」", "PM7.ini");
                MyComputer.FileSystem.RenameFile("「HDATE_YYYYMMDD」", "HDATE.ini");
                MyComputer.FileSystem.RenameFile("「VFREPORT_YYYYMMDD」", "VFREPORT.ini");
            }
            catch (Exception)
            {
                
            }
        }



        //****************   待完成内容    *******************
        //1. 异常处理内容,对应文件恢复处理
        //2. 日志记录
        //3. 相关细节验证
        //4. pm7对象域具体对应更新
        //5. 输入验证!!!
        //6. 共同方法及变量提取

        //周日已做
        //1.pm7具体对象域更新


        //Q:
        //1.设计书要求JpnYears和WDATETIM最新元号从画面获取,如何实现?


    }
}
上一篇下一篇

猜你喜欢

热点阅读