Exemplo n.º 1
0
        private void btPackage_Click(object sender, EventArgs e)
        {
            string src  = tbPath.Text;
            string file = tbName.Text + "-" + tbVersion.Text.Replace('.', '-') + ".udp";
            string dst  = DirTool.Combine(DirTool.GetFilePath(tbPath.Text), file);

            if (FilePackageTool.Pack(src, dst) > 0 && File.Exists(dst))
            {
                string md5Code = FileTool.GetMD5(dst);
                //设置更新模型,14个属性
                AppUpdateInfo aui = new AppUpdateInfo()
                {
                    Name         = tbName.Text,
                    Author       = tbAuthor.Text,
                    Desc         = tbDesc.Text,
                    Version      = tbVersion.Text,
                    ReleasePath  = tbReleasePath.Text,
                    Necessary    = cbNecessary.Checked,
                    DateTime     = DateTime.Now,
                    DownloadMode = rbHttpMode.Checked ? 0 : 1,
                    HttpUrl      = tbHttpUrl.Text,
                    FtpIp        = tbFtpIp.Text,
                    FtpAccount   = tbFtpAccount.Text,
                    FtpPassword  = tbFtpPassword.Text,
                    FtpFile      = tbFtpFile.Text,
                    Md5          = md5Code,
                };
                string auiJson = JsonTool.ToStr(aui);
                TxtTool.Create(dst + ".txt", auiJson);
            }
        }
Exemplo n.º 2
0
        private void BTPack_Click(object sender, EventArgs e)
        {
            string src  = TBPath.Text;
            string file = Path.GetFileName(src) + ".package";
            string dst  = DirTool.Combine(DirTool.GetFilePath(TBPath.Text), file);

            FilePackageTool.Pack(src, dst);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 定时处理要备份的文件任务
        /// </summary>
        private void BackupFileTask()
        {
            while (IsStart)
            {
                if (ListTool.HasElements(BackupFiles))
                {
                    //获取要备份的文件列表并复制样本
                    List <string> temp;
                    lock (BackupFiles)
                    {
                        temp        = BackupFiles;
                        BackupFiles = new List <string>();
                    }

                    using (var db = new Muse())
                    {
                        foreach (var t in temp)
                        {
                            //要备份的文件存在
                            if (File.Exists(t))
                            {
                                //文件属于要备份的文件目录
                                string      filepath = DirTool.GetFilePath(t);
                                BackupPaths path     = Paths.FirstOrDefault(x => filepath.Contains(x.Path));
                                if (path != null)
                                {
                                    //文件的MD5码以前没有备份过
                                    string md5    = FileTool.GetMD5(t);
                                    bool   isback = db.Any <BackupFiles>(x => x.FullPath == t && x.Md5 == md5, null);
                                    if (!isback)
                                    {
                                        string pathname  = path.Path;                                                                           //备份文件夹路径
                                        string pathalias = path.Alias;                                                                          //备份文件夹别名
                                        string pathfile  = t.Substring(pathname.Length, t.Length - pathname.Length);                            //截取备份文件子目录(相对备份文件夹)
                                        string fileext   = "." + DateTimeConvert.CompactString(DateTime.Now) + Path.GetExtension(t);            //设置后缀
                                        string fullpath  = DirTool.Combine(R.Settings.FileBackup.FileManBackup, pathalias, pathfile + fileext); //组合路径

                                        //删除冗余
                                        DeleteExcess(t);
                                        //备份文件
                                        BackupFile(t, fullpath, md5);
                                        _FileCount++;
                                    }
                                }
                            }
                        }
                    }
                }
                Thread.Sleep(R.Settings.FileBackup.BACK_UP_INTERVAL);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 启动最新版本程序
        /// </summary>
        /// <param name="route">路径:程序版本文件夹路径</param>
        /// <param name="startfilename">可执行文件名</param>
        /// <returns></returns>
        public static bool StartNewVersion(string route, string startfilename)
        {
            //判断路径是文件还是文件夹,并统一处理为文件夹
            string appPath = route;

            if (FileTool.IsFile(route))
            {
                appPath = DirTool.GetFilePath(route);
            }

            if (Directory.Exists(appPath))
            {
                //获取运行目录下所有文件
                List <string> paths = DirTool.GetPath(appPath);
                if (ListTool.HasElements(paths))
                {
                    //解析属于版本号的文件
                    Version version   = null;
                    string  startfile = null;
                    foreach (var path in paths)
                    {
                        //只解析文件名带三个点的文件夹
                        string filename = Path.GetFileName(path);
                        if (StringTool.SubStringCount(filename, ".") == 3)
                        {
                            try
                            {
                                Version tempVersion = new Version(filename);
                                string  tempFile    = DirTool.Combine(path, startfilename);
                                if ((version == null || tempVersion > version) && File.Exists(tempFile))
                                {
                                    version   = tempVersion;
                                    startfile = tempFile;
                                }
                            }
                            catch { }
                        }
                    }
                    //准备启动
                    if (startfile != null)
                    {
                        return(ProcessTool.Start(startfile));
                    }
                }
            }
            return(false);
        }
Exemplo n.º 5
0
 public static bool Copy(string sourceFileName, string destFileName, bool overwrite)
 {
     if (File.Exists(sourceFileName))
     {
         string destPath = DirTool.GetFilePath(destFileName);
         if (DirTool.Create(destPath))
         {
             try
             {
                 File.Copy(sourceFileName, destFileName, overwrite);
                 return(true);
             }
             catch { }
         }
     }
     return(false);
 }
Exemplo n.º 6
0
        /// <summary>
        /// 获取最新版本号
        /// </summary>
        /// <param name="route">路径:程序版本文件夹路径</param>
        /// <param name="exeFile">可执行文件名</param>
        /// <returns></returns>
        public static bool GetNewVersion(string route, string exeFile, out Version version, out string startFile)
        {
            version   = null;
            startFile = "";

            //判断路径是文件还是文件夹,并统一处理为文件夹
            string appPath = route;

            if (FileTool.IsFile(route))
            {
                appPath = DirTool.GetFilePath(route);
            }

            if (Directory.Exists(appPath))
            {
                //获取运行目录下所有文件
                List <string> paths = DirTool.GetPath(appPath);
                if (ListTool.HasElements(paths))
                {
                    foreach (var path in paths)
                    {
                        //只解析文件名带三个点的文件夹
                        string filename = Path.GetFileName(path);
                        if (StringTool.SubStringCount(filename, ".") == 3)
                        {
                            try
                            {
                                Version tempVersion = new Version(filename);
                                string  tempFile    = DirTool.Combine(path, exeFile);
                                if ((version == null || tempVersion > version) && File.Exists(tempFile))
                                {
                                    version   = tempVersion;
                                    startFile = tempFile;
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            if (version != null && Str.Ok(startFile))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 查询是否有最新版本程序可以执行
        /// </summary>
        /// <param name="route">路径:程序版本文件夹路径</param>
        /// <param name="startfilename">可执行文件名</param>
        /// <returns></returns>
        public static bool HasNewVersion(string route, string startfilename)
        {
            //判断路径是文件还是文件夹,并统一处理为文件夹
            string appPath = route;

            if (FileTool.IsFile(route))
            {
                appPath = DirTool.GetFilePath(route);
            }

            if (Directory.Exists(appPath))
            {
                //获取运行目录下所有文件
                List <string> paths = DirTool.GetPath(appPath);
                if (ListTool.HasElements(paths))
                {
                    //解析属于版本号的文件
                    foreach (var path in paths)
                    {
                        //只解析文件名带三个点的文件夹
                        string filename = Path.GetFileName(path);
                        if (StringTool.SubStringCount(filename, ".") == 3)
                        {
                            try
                            {
                                //有版本命名的文件,且文件中有exe程序
                                Version tempVersion = new Version(filename);
                                string  tempFile    = DirTool.Combine(path, startfilename);
                                if (File.Exists(tempFile))
                                {
                                    return(true);
                                }
                            }
                            catch { }
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 创建打包文件列表信息
        /// </summary>
        /// <param name="files"></param>
        /// <param name="srcPath"></param>
        /// <returns></returns>
        private static List <FilePackageModel> CreateFilePackageModel(List <string> files, string srcPath)
        {
            if (ListTool.IsNullOrEmpty(files))
            {
                return(null);
            }

            List <FilePackageModel> result = new List <FilePackageModel>();

            //汇总所有文件
            files.ForEach(x =>
            {
                result.Add(new FilePackageModel()
                {
                    Name = Path.GetFileName(x),
                    Path = DirTool.GetFilePath(x).Substring(srcPath.Count()),
                    Size = FileTool.Size(x),
                    MD5  = FileTool.GetMD5(x),
                });
            });
            return(result);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 更新回滚
        /// </summary>
        /// <param name="vm"></param>
        private void UpdateRollback(VersionModel vm)
        {
            var backFile = vm.FileList.Where(x => x.IsClean == false);

            foreach (var file in backFile)
            {
                string tempBack  = DirTool.Combine(R.Paths.Temp, BackupTemp, file.ServerFile);                                               //备份到目标位置(带文件名)
                string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.ProjectRoot, file.LocalFile); //旧文件位置

                //还原备份文件
                if (File.Exists(tempBack))
                {
                    try
                    {
                        DirTool.Create(DirTool.GetFilePath(localFile));
                        File.Copy(tempBack, localFile, true);
                        UIUpdateDetail("正在还原备份文件:" + Path.GetFileName(file.LocalFile));
                    }
                    catch (Exception e) { }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// 备份并替换文件
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        private bool UpdateInsteadAndBackup(VersionModel vm)
        {
            var insteadFile = vm.FileList.Where(x => x.IsClean == false);

            foreach (var file in insteadFile)
            {
                string tempDown  = DirTool.Combine(R.Paths.Temp, DownTemp, file.ServerFile);                                                 //下载到目标位置(带文件名)
                string tempBack  = DirTool.Combine(R.Paths.Temp, BackupTemp, file.ServerFile);                                               //备份到目标位置(带文件名)
                string localFile = DirTool.IsDriver(file.LocalFile) ? file.LocalFile : DirTool.Combine(R.Paths.ProjectRoot, file.LocalFile); //旧文件位置

                //备份文件
                if (File.Exists(localFile) && File.Exists(tempDown))
                {
                    try
                    {
                        DirTool.Create(DirTool.GetFilePath(tempBack));
                        File.Copy(localFile, tempBack, true);
                        UIUpdateDetail("正在备份:" + Path.GetFileName(tempBack));
                    }
                    catch (Exception e) { }
                }
                //替换文件
                if (File.Exists(tempDown))
                {
                    try
                    {
                        DirTool.Create(DirTool.GetFilePath(localFile));
                        File.Copy(tempDown, localFile, true);
                        UIUpdateDetail("正在更新:" + Path.GetFileName(file.LocalFile));
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 11
0
 private void BtRestoreToOld_Click(object sender, EventArgs e)
 {
     if (DgvFiles.CurrentRow != null && DgvFiles.CurrentRow.Index >= 0)
     {
         BackupFiles file  = Files[DgvFiles.CurrentRow.Index];
         string      title = string.Format("文件还原", file.LastWriteTime);
         string      text  = string.Format("您确定将文件:{0} [ {1} ]{2}还原到:{2}{3} 吗?", Path.GetFileName(file.FullPath), file.LastWriteTime, Environment.NewLine, file.FullPath);
         if (MessageBox.Show(text, title, MessageBoxButtons.OKCancel) == DialogResult.OK)
         {
             string from   = file.BackupFullPath;
             string to     = file.FullPath;
             string topath = DirTool.GetFilePath(to);
             if (DirTool.Create(topath))
             {
                 File.Copy(from, to, true);
             }
             else
             {
                 MessageBox.Show(string.Format("路径:{0} 不存在,请还原到其他路径。", topath), "路径不存在");
             }
         }
     }
 }
Exemplo n.º 12
0
 public bool Download(string ftpFile, string localFile, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
 {
     try
     {
         long   current = 0, filesize = GetFileSize(ftpFile);
         string localPath = DirTool.GetFilePath(localFile);
         if (!Directory.Exists(localPath))
         {
             Directory.CreateDirectory(localPath);
         }
         string        uri = Path.Combine(FtpUri, ftpFile);
         FtpWebRequest ftp = GetRequest(uri);
         ftp.Method = WebRequestMethods.Ftp.DownloadFile;
         using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
         {
             using (Stream responseStream = response.GetResponseStream())
             {
                 using (FileStream fs = new FileStream(localFile, FileMode.Create))
                 {
                     byte[] buffer = new byte[1024 * 1024];
                     int    read   = 0;
                     do
                     {
                         read = responseStream.Read(buffer, 0, buffer.Length);
                         fs.Write(buffer, 0, read);
                         current += read;
                         progress?.Invoke(sender, new ProgressEventArgs(current, filesize));
                     } while (!(read == 0));
                     fs.Flush();
                 }
             }
         }
         return(true);
     }
     catch { }
     return(false);
 }
Exemplo n.º 13
0
 /// <summary>
 /// 备份文件
 /// </summary>
 /// <param name="path"></param>
 /// <param name="newpath"></param>
 private void BackupFile(string path, string newpath, string md5)
 {
     using (var db = new Muse())
     {
         try
         {
             if (DirTool.Create(DirTool.GetFilePath(newpath)))
             {
                 string lastwritetime = DateTimeConvert.StandardString(File.GetLastWriteTime(path));
                 File.Copy(path, newpath, true);
                 db.Add(new BackupFiles()
                 {
                     FullPath       = path,
                     BackupFullPath = newpath,
                     Size           = FileTool.Size(path),
                     BackupTime     = DateTimeConvert.StandardString(DateTime.Now),
                     LastWriteTime  = lastwritetime,
                     Md5            = md5,
                 });
             }
         }
         catch (Exception e) { }
     }
 }
Exemplo n.º 14
0
        /// <summary>
        /// 文件打包
        /// </summary>
        /// <param name="srcPath">要打包的路径</param>
        /// <param name="dstFile">打包后的文件</param>
        /// <param name="progress">回调进度</param>
        /// <param name="overwrite">覆盖打包后的文件(重复时)</param>
        /// <returns>
        /// -11;//要打包的路径不存在
        /// -12;//打包后的目标文件已存在
        /// -13;//要打包的路径中没有文件
        /// -14;//输出文件夹不存在
        /// -404;//未知错误,操作失败
        /// </returns>
        public static int Pack(string srcPath, string dstFile, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
        {
            DateTime beginTime = DateTime.Now;

            if (!Directory.Exists(srcPath))
            {
                return(-11);                           //要打包的路径不存在
            }
            if (File.Exists(dstFile) && !overwrite)
            {
                return(-12);                                   //打包后的目标文件已存在
            }
            if (!DirTool.Create(DirTool.GetFilePath(dstFile)))
            {
                return(-14);                                              //输出文件夹不存在
            }
            List <string>           tempfiles = FileTool.GetAllFile(srcPath);
            List <FilePackageModel> files     = CreateFilePackageModel(tempfiles, srcPath);

            if (ListTool.HasElements(files))
            {
                long allfilesize     = files.Sum(x => x.Size); //文件总大小
                long surplusfilesize = allfilesize;            //剩余要写入的文件大小
                using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
                {
                    try
                    {
                        //写入文件类型标识和版本号
                        byte[] filetypeandversion = Encoding.Default.GetBytes(FileType + FileVersion);
                        fsWrite.Write(filetypeandversion, 0, filetypeandversion.Length);

                        //写入头部总长度
                        int    headl      = files.Sum(x => x.AllByteLength);
                        byte[] headlength = BitConverter.GetBytes(headl);
                        fsWrite.Write(headlength, 0, headlength.Length);
                        //循环写入文件信息
                        files.ForEach(x =>
                        {
                            fsWrite.Write(x.NameLengthByte, 0, x.NameLengthByte.Length);
                            fsWrite.Write(x.NameByte, 0, x.NameByte.Length);
                            fsWrite.Write(x.PathLengthByte, 0, x.PathLengthByte.Length);
                            fsWrite.Write(x.PathByte, 0, x.PathByte.Length);
                            fsWrite.Write(x.SizeLengthByte, 0, x.SizeLengthByte.Length);
                            fsWrite.Write(x.SizeByte, 0, x.SizeByte.Length);
                            fsWrite.Write(x.MD5LengthByte, 0, x.MD5LengthByte.Length);
                            fsWrite.Write(x.MD5Byte, 0, x.MD5Byte.Length);
                        });
                        //循环写入文件
                        files.ForEach(x =>
                        {
                            //读取文件(可访问被打开的exe文件)
                            using (FileStream fsRead = new FileStream(DirTool.Combine(srcPath, x.Path, x.Name), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                            {
                                int readCount = 0;
                                byte[] buffer = new byte[FileBuffer];
                                while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    fsWrite.Write(buffer, 0, readCount);
                                    surplusfilesize -= readCount;
                                    progress?.Invoke(sender, new ProgressEventArgs(allfilesize - surplusfilesize, allfilesize));
                                }
                            }
                        });
                    }
                    catch (Exception e) { }
                }
                if (surplusfilesize == 0)
                {
                    return((int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds));//操作成功
                }
            }
            else
            {
                return(-13);//要打包的路径中没有文件
            }
            //打包失败后,删除打包后的文件
            try { File.Delete(dstFile); } catch (Exception e) { }
            return(-404);//未知错误,操作失败
        }
Exemplo n.º 15
0
        /// <summary>
        /// 获取要更新的插件列表
        /// </summary>
        /// <returns></returns>
        public static List <PluginModel> GetPluginList()
        {
            #region 本地插件列表是否存在
            if (!File.Exists(R.Files.Plugins))
            {
                DirTool.Create(DirTool.GetFilePath(R.Files.Plugins));

                //如果文件不存在 创建新的插件xml
                XElement xe = new XElement("Plugins");
                xe.Save(R.Files.Plugins);
            }
            #endregion
            #region 读取本地插件列表
            List <PluginModel> localPluginList = new List <PluginModel>();
            try
            {
                XElement xe = XElement.Load(R.Files.Plugins);
                IEnumerable <XElement> elements = xe.Elements("Item");
                if (ListTool.HasElements(elements))
                {
                    foreach (var ele in elements)
                    {
                        PluginModel plug = new PluginModel()
                        {
                            Name    = ele.Attribute("Name").Value,
                            Version = ele.Attribute("Version").Value
                        };
                        localPluginList.Add(plug);
                    }
                }
            }
            catch (Exception e) { }
            #endregion
            #region 读取服务器插件列表
            List <PluginModel> serverPluginList = HttpTool.Get <List <PluginModel> >(R.Settings.Url.WebService + "getPluginsList");
            #endregion
            #region 整理需要更新的插件列表
            List <PluginModel> rs = new List <PluginModel>();
            if (ListTool.HasElements(serverPluginList))
            {
                serverPluginList.ForEach(p =>
                {
                    var tmp = localPluginList.FirstOrDefault(x => x.Name == p.Name);
                    if (tmp == null)
                    {
                        //如果服务器有的插件,本地没有,则添加至需要更新的列表
                        rs.Add(p);
                    }
                    else
                    {
                        if (tmp.Version != p.Version)
                        {
                            //如果服务器插件版本和本机插件版本不同,则添加至需要更新列表
                            rs.Add(p);
                        }
                    }
                });
            }
            #endregion
            return(rs);
        }