Ejemplo n.º 1
0
        /// <summary>
        /// 备份文件(只保留近十个文件)
        /// </summary>
        /// <param name="FileName">需要备份的文件</param>
        private void BackupFiles(List <string> FileName)
        {
            if (!Directory.Exists(BackupFile))
            {
                Directory.CreateDirectory(BackupFile);
            }

            Dictionary <string, DateTime> File_CreateTime = new Dictionary <string, DateTime>();

            string[] dirs = Directory.GetDirectories(BackupFile);//获取子文件夹
            foreach (string dir in dirs)
            {
                File_CreateTime.Add(dir, File.GetCreationTime(dir));
            }
            var dicSort    = from objDic in File_CreateTime orderby objDic.Value descending select objDic;//Dictonary排序(降序) 如果需要升序  descending 去掉即可
            int FilesCount = 10;

            foreach (KeyValuePair <string, DateTime> kvp in dicSort)
            {
                if (--FilesCount < 0)
                {
                    Directory.Delete(kvp.Key, true);
                }
            }

            MyFunction MyFunction   = new MyFunction();
            string     savepath     = MyFunction.GetVersion(AppDomain.CurrentDomain.BaseDirectory + @"\" + AppName);
            string     createfloder = CreateFolder(BackupFile + @"\", savepath);

            while (FileName.Count > 0)
            {
                try
                {
                    string file = FileName[0];
                    if (!File.Exists(file))
                    {
                        FileName.RemoveAt(0);
                        continue;
                    }
                    File.Copy(file, createfloder + @"\" + file);
                    FileName.RemoveAt(0);
                }
                catch (Exception ex)
                {
                    ShowStr("备份文件异常:" + ex.Message);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// PC软件升级方法
        /// </summary>
        public void PCUpdate()
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            try
            {
                string UpdateProgram = UpdateTempFile + @"\" + AppName;
                if (!File.Exists(UpdateProgram))
                {
                    ShowStr("升级程序丢失");
                    return;
                }

                string tempcopyname = "%($)%.exe";//临时复制的文件,不需要复制此文件
                File.Copy(UpdateProgram, this.ApplicationRunPath + @"\" + tempcopyname, true);
                //UpdateVersion(UpdateProgram, this.ApplicationRunPath + @"\" + AppName);
                string filePath = this.ApplicationRunPath + @"\" + tempcopyname;
                startInfo.FileName = filePath;
                //参数0为源文件夹,参数1为目标文件夹,参数2为复制完毕后运行程序
                string parameter = string.Format("{0} {1} {2}"
                                                 , this.ApplicationRunPath + @"\" + UpdateTempFile
                                                 , this.ApplicationRunPath
                                                 , AppName);
                startInfo.Arguments   = parameter;
                startInfo.WindowStyle = ProcessWindowStyle.Normal;
                Process.Start(startInfo);
            }
            catch (Exception ex)
            {
#if DEBUG
                MessageBox.Show(ex.Message, "升级故障");
#endif
                MyFunction.AppendUpdate_LOG("升级故障:" + ex.Message);
            }
            finally
            {
                Environment.Exit(0);
            }
        }