ソースを参照

添加启动器功能

yuzhengyang 8 年 前
コミット
1a0abed439

+ 12 - 0
Fork.Net/Test/Y.Test/Views/TestUpdateForm.Designer.cs

@@ -33,6 +33,7 @@
             this.label1 = new System.Windows.Forms.Label();
             this.label2 = new System.Windows.Forms.Label();
             this.button1 = new System.Windows.Forms.Button();
+            this.label3 = new System.Windows.Forms.Label();
             this.SuspendLayout();
             // 
             // progressBar1
@@ -77,11 +78,21 @@
             this.button1.UseVisualStyleBackColor = true;
             this.button1.Click += new System.EventHandler(this.button1_Click);
             // 
+            // label3
+            // 
+            this.label3.AutoSize = true;
+            this.label3.Location = new System.Drawing.Point(13, 231);
+            this.label3.Name = "label3";
+            this.label3.Size = new System.Drawing.Size(41, 12);
+            this.label3.TabIndex = 5;
+            this.label3.Text = "label3";
+            // 
             // TestUpdateForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(284, 261);
+            this.Controls.Add(this.label3);
             this.Controls.Add(this.button1);
             this.Controls.Add(this.label2);
             this.Controls.Add(this.label1);
@@ -102,5 +113,6 @@
         private System.Windows.Forms.Label label1;
         private System.Windows.Forms.Label label2;
         private System.Windows.Forms.Button button1;
+        private System.Windows.Forms.Label label3;
     }
 }

+ 22 - 7
Fork.Net/Test/Y.Test/Views/TestUpdateForm.cs

@@ -5,12 +5,14 @@ using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 using Y.Test.Commons;
-using Y.Utils.AppUtils.UpdateUtils;
 using Y.Utils.DataUtils.JsonUtils;
 using Y.Utils.DelegateUtils;
+using Y.Utils.IOUtils.FileUtils;
 using Y.Utils.IOUtils.TxtUtils;
+using Y.Utils.UpdateUtils;
 
 namespace Y.Test.Views
 {
@@ -23,22 +25,27 @@ namespace Y.Test.Views
 
         private void TestUpdateForm_Load(object sender, EventArgs e)
         {
-
+            FilePackageTool.Pack(@"D:\Temp\AppUpdate\Temp\6.0.0.2版本打包", @"D:\FTP\AppUpdateBag\Oreo.NetMan[6.0.0.2].updatebag");
         }
 
         private void button1_Click(object sender, EventArgs e)
         {
-            AppUpdateTool aut = new AppUpdateTool();
-            aut.Update("Oreo.NetMan", new Version(Application.ProductVersion),
-                "http://localhost:20001/Update/Get?name=lalala",
-                R.Paths.Temp, R.Paths.Relative,
-                UIDownProgress, null, UIUnpackProgress, null);
+            Task.Factory.StartNew(() =>
+            {
+                AppUpdateTool aut = new AppUpdateTool();
+                int updateCode = aut.Update("Oreo.NetMan", new Version(Application.ProductVersion),
+                    "http://localhost:20001/Update/Get?name=lalala",
+                    R.Paths.Temp, R.Paths.Relative,
+                    UIDownProgress, null, UIUnpackProgress, null);
+                UIUpdateCode(updateCode);
+            });
         }
         private void UIDownProgress(object sender, ProgressEventArgs e)
         {
             BeginInvoke(new Action(() =>
             {
                 progressBar1.Value = (int)(e.Current * 100 / e.Total);
+                label1.Text = string.Format("{0} / {1}", e.Current, e.Total);
             }));
         }
         private void UIUnpackProgress(object sender, ProgressEventArgs e)
@@ -46,6 +53,14 @@ namespace Y.Test.Views
             BeginInvoke(new Action(() =>
             {
                 progressBar2.Value = (int)(e.Current * 100 / e.Total);
+                label2.Text = string.Format("{0} / {1}", e.Current, e.Total);
+            }));
+        }
+        private void UIUpdateCode(int code)
+        {
+            BeginInvoke(new Action(() =>
+            {
+                label3.Text = code.ToString();
             }));
         }
     }

+ 60 - 0
Fork.Net/Y.Utils/AppUtils/AppLaunchTool.cs

@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using Y.Utils.DataUtils.Collections;
+using Y.Utils.DataUtils.StringUtils;
+using Y.Utils.IOUtils.PathUtils;
+using Y.Utils.WindowsUtils.ProcessUtils;
+
+namespace Y.Utils.AppUtils
+{
+    public static class AppLaunchTool
+    {
+        public static bool Start(string appfile, string startfilename)
+        {
+            if (File.Exists(appfile))
+            {
+                //获取程序运行目录
+                string exePath = DirTool.GetFilePath(appfile);
+                if (Directory.Exists(exePath))
+                {
+                    //获取运行目录下所有文件
+                    List<string> paths = DirTool.GetPath(exePath);
+                    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;
+        }
+    }
+}

+ 34 - 4
Fork.Net/Y.Utils/DataUtils/StringUtils/StringTool.cs

@@ -1,7 +1,10 @@
-//############################################################
+//************************************************************************
 //      https://github.com/yuzhengyang
-//      author:yuzhengyang
-//############################################################
+//      author:     yuzhengyang
+//      date:       2017.3.29 - 2017.8.3
+//      desc:       字符串工具类
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
 using System;
 using System.Collections.Generic;
 using System.Text;
@@ -9,8 +12,13 @@ using Y.Utils.DataUtils.Collections;
 
 namespace Y.Utils.DataUtils.StringUtils
 {
-    public class StringTool
+    public sealed class StringTool
     {
+        /// <summary>
+        /// 判断字符串为null或为空格
+        /// </summary>
+        /// <param name="str"></param>
+        /// <returns></returns>
         public static bool IsNullOrWhiteSpace(string str)
         {
             if (str == null)
@@ -20,6 +28,13 @@ namespace Y.Utils.DataUtils.StringUtils
 
             return false;
         }
+        /// <summary>
+        /// 分割字符串
+        /// </summary>
+        /// <param name="str"></param>
+        /// <param name="separator"></param>
+        /// <param name="result"></param>
+        /// <returns></returns>
         public static int Split(string str, char separator, out string[] result)
         {
             if (!string.IsNullOrWhiteSpace(str))
@@ -34,5 +49,20 @@ namespace Y.Utils.DataUtils.StringUtils
             result = null;
             return 0;
         }
+        /// <summary>
+        /// 字符串中字符出现次数
+        /// </summary>
+        /// <param name="s"></param>
+        /// <param name="sub"></param>
+        /// <returns></returns>
+        public static int SubStringCount(string s, string sub)
+        {
+            if (s.Contains(sub))
+            {
+                string sReplaced = s.Replace(sub, "");
+                return (s.Length - sReplaced.Length) / sub.Length;
+            }
+            return 0;
+        }
     }
 }

+ 2 - 2
Fork.Net/Y.Utils/IOUtils/FileUtils/FilePackageTool.cs

@@ -120,7 +120,7 @@ namespace Y.Utils.IOUtils.FileUtils
         /// -11; //要解包的文件不存在
         /// -12;//要解包的目标文件夹已存在
         /// -20;// 文件类型不匹配
-        /// -404;//未知错误,操作失败
+        /// -99;//未知错误,操作失败
         /// </returns>
         public static int Unpack(string srcFile, string dstPath, ProgressDelegate.ProgressHandler progress = null, object sender = null, bool overwrite = true)
         {
@@ -197,7 +197,7 @@ namespace Y.Utils.IOUtils.FileUtils
                 }
                 catch (Exception e) { }
             }
-            return -404;//未知错误,操作失败
+            return -99;//未知错误,操作失败
         }
 
 

+ 1 - 1
Fork.Net/Y.Utils/UpdateUtils/AppUpdateInfo.cs

@@ -1,7 +1,7 @@
 //************************************************************************
 //      https://github.com/yuzhengyang
 //      author:     yuzhengyang
-//      date:       2017.8.1 - 2017.8.1
+//      date:       2017.8.1 - 2017.8.3
 //      desc:       程序更新工具模型
 //      Copyright (c) yuzhengyang. All rights reserved.
 //************************************************************************

+ 12 - 11
Fork.Net/Y.Utils/UpdateUtils/AppUpdateTool.cs

@@ -37,11 +37,11 @@ namespace Y.Utils.UpdateUtils
         /// <param name="releaseprogress">释放进度回调</param>
         /// <param name="releasesender">释放进度事件数据</param>
         /// <returns>
-        /// -10;//无最新版本,停止操作
-        /// -20;//请求服务器最新版本失败
-        /// -30;//新版本号格式不正确,解析失败
-        /// -40;//文件下载失败
-        /// -50;//文件释放失败
+        /// -10000;//无最新版本,停止操作
+        /// -20000;//请求服务器最新版本失败
+        /// -30000;//新版本号格式不正确,解析失败
+        /// -40000;//文件下载失败
+        /// -50000;//文件释放失败
         /// </returns>
         public int Update(string name, Version version, string url, string path, Dictionary<string, string> dictionary,
             ProgressDelegate.ProgressHandler downprogress = null, object downsender = null,
@@ -54,7 +54,7 @@ namespace Y.Utils.UpdateUtils
             if (info != null)
             {
                 Version newVersion = GerVersion(info.Version);
-                if (newVersion == null) return -30;//新版本号格式不正确,解析失败
+                if (newVersion == null) return -30000;//新版本号格式不正确,解析失败
 
                 if (newVersion > version)
                 {
@@ -66,29 +66,30 @@ namespace Y.Utils.UpdateUtils
                         //格式化释放文件目录
                         string releasepath = AppDirTool.Get(info.ReleasePath, dictionary);
                         //释放文件
-                        if (FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender) > 0)
+                        int unpackCode = 0;
+                        if ((unpackCode = FilePackageTool.Unpack(downfile, releasepath, releaseprogress, releasesender)) > 0)
                         {
                             stopwatch.Stop();
                             return (int)stopwatch.Elapsed.TotalSeconds;
                         }
                         else
                         {
-                            return -50;//文件释放失败
+                            return -50000 + unpackCode;//文件释放失败
                         }
                     }
                     else
                     {
-                        return -40;//文件下载失败
+                        return -40000;//文件下载失败
                     }
                 }
                 else
                 {
-                    return -10;//无最新版本,停止操作
+                    return -10000;//无最新版本,停止操作
                 }
             }
             else
             {
-                return -20;//请求服务器最新版本失败
+                return -20000;//请求服务器最新版本失败
             }
         }
         private Version GerVersion(string s)

+ 18 - 0
Fork.Net/Y.Utils/WindowsUtils/ProcessUtils/ProcessTool.cs

@@ -64,6 +64,24 @@ namespace Y.Utils.WindowsUtils.ProcessUtils
             }
         }
 
+        public static bool Start(string file, string args = "")
+        {
+            try
+            {
+                if (File.Exists(file))
+                {
+                    Process p = new Process();
+                    p.StartInfo.FileName = file;
+                    p.StartInfo.Arguments = "";
+                    p.StartInfo.UseShellExecute = true;
+                    p.Start();
+                    p.WaitForInputIdle(3000);
+                    return true;
+                }
+            }
+            catch (Exception ex) { }
+            return false;
+        }
         public static void Starts(string[] files)
         {
             if (ListTool.HasElements(files))

+ 1 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -51,6 +51,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AppUtils\AppLaunchTool.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="UpdateUtils\AppUpdateInfo.cs" />
     <Compile Include="UpdateUtils\AppUpdateTool.cs" />