Browse Source

设置记录程序启动次数

yuzhengyang 8 years ago
parent
commit
850f1c89ff

+ 2 - 0
Fork.Net/Oreo.Plugins/Oreo.FileMan/Oreo.FileMan.csproj

@@ -22,6 +22,7 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <UseVSHostingProcess>false</UseVSHostingProcess>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -34,6 +35,7 @@
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
+    <Reference Include="System.Configuration" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />

+ 13 - 0
Fork.Net/Oreo.Plugins/Oreo.FileMan/Views/MainForm.Designer.cs

@@ -33,6 +33,7 @@
             this.button3 = new System.Windows.Forms.Button();
             this.button4 = new System.Windows.Forms.Button();
             this.BtFileDecrypt = new System.Windows.Forms.Button();
+            this.button1 = new System.Windows.Forms.Button();
             this.SuspendLayout();
             // 
             // BtFileEncrypt
@@ -83,11 +84,22 @@
             this.BtFileDecrypt.UseVisualStyleBackColor = true;
             this.BtFileDecrypt.Click += new System.EventHandler(this.BtFileDecrypt_Click);
             // 
+            // button1
+            // 
+            this.button1.Location = new System.Drawing.Point(158, 99);
+            this.button1.Name = "button1";
+            this.button1.Size = new System.Drawing.Size(75, 23);
+            this.button1.TabIndex = 5;
+            this.button1.Text = "button1";
+            this.button1.UseVisualStyleBackColor = true;
+            this.button1.Click += new System.EventHandler(this.button1_Click);
+            // 
             // MainForm
             // 
             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.button1);
             this.Controls.Add(this.BtFileDecrypt);
             this.Controls.Add(this.button4);
             this.Controls.Add(this.button3);
@@ -107,5 +119,6 @@
         private System.Windows.Forms.Button button3;
         private System.Windows.Forms.Button button4;
         private System.Windows.Forms.Button BtFileDecrypt;
+        private System.Windows.Forms.Button button1;
     }
 }

+ 54 - 18
Fork.Net/Oreo.Plugins/Oreo.FileMan/Views/MainForm.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Configuration;
 using System.Data;
 using System.Drawing;
 using System.IO;
@@ -83,27 +84,62 @@ namespace Oreo.FileMan.Views
 
         private void MainForm_Load(object sender, EventArgs e)
         {
-            ////打包
-            //Dictionary<string, object> dicToPack = new Dictionary<string, object>();
-            ////dicToPack.Add("key1", Image.FromFile(@"D:\temp\测试打包\1511925984.jpeg"));
-            ////dicToPack.Add("key2", Image.FromFile(@"D:\temp\测试打包\1555714799.jpeg"));
-            //dicToPack.Add("key1", FilePackageTool.FileDeSerialize(@"D:\temp\测试打包\新建文件夹\新建文本文档 1.txt"));
-            //dicToPack.Add("key2", FilePackageTool.FileDeSerialize(@"D:\temp\测试打包\新建文件夹\新建文本文档 2.txt"));
-            //dicToPack.Add("key3", "hello world");
-            //FilePackageTool.ResourcePackage(dicToPack, @"D:\temp\测试打包\pkg1.pkg");
-            ////解包
-            ////Dictionary<string, object> dicRcv = FilePackageTool.ResourceUnpack(@"D:\temp\测试打包\pkg1.pkg");
+            bool flag = CanUpdate();
         }
-        
+
         private void button2_Click(object sender, EventArgs e)
         {
-            //string a = @"D:\Temp\测试压缩\Root\Plugin\NetAssist.exe";
-            //FileCompressTool.Compress(a);
+            FilePackageTool.Pack(@"D:\Temp\测试压缩\Root", @"D:\Temp\测试压缩\Root.pkg");
+        }
+
+        private void button1_Click(object sender, EventArgs e)
+        {
+            FilePackageTool.Unpack(@"D:\Temp\测试压缩\Root.pkg", @"D:\Temp\测试压缩\Root");
+        }
 
-            //string b = @"D:\Temp\测试压缩\Root\Plugin\NetAssist.exe.gz";
-            //FileCompressTool.Decompress(b);
-            //FilePackageTool.Pack(@"D:\temp\测试打包\新建文件夹", @"D:\temp\测试打包\新建文件夹.pkg");
-            FilePackageTool.Unpack(@"D:\temp\测试打包\新建文件夹.pkg", @"D:\temp\测试打包\新建文件夹");
-        } 
+        private bool CanUpdate()
+        {
+            string key = "TodayUpdateTimes";
+            DateTime today = DateTime.Parse(string.Format("{0}-{1}-{2} 00:00:00", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
+            DateTime setday = today;
+
+            //读取配置
+            string temp = ConfigurationManager.AppSettings[key];
+            if (DateTime.TryParse(temp, out setday) && setday >= today && setday <= today.AddDays(1))
+            {
+                if (setday.Hour < 5)
+                    CanUpdateSetConfig(key, setday.AddHours(1).ToString());//累加hour记录次数
+                else
+                    return false;
+            }
+            else
+            {
+                //配置失效,设置为默认值
+                CanUpdateSetConfig(key, today.ToString());
+            }
+            return true;
+        }
+        private bool CanUpdateSetConfig(string key, string value)
+        {
+            try
+            {
+                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+                if (config.AppSettings.Settings.AllKeys.Contains(key))
+                {
+                    config.AppSettings.Settings[key].Value = value;
+                }
+                else
+                {
+                    config.AppSettings.Settings.Add(key, value);
+                }
+                config.Save(ConfigurationSaveMode.Modified);
+                ConfigurationManager.RefreshSection("appSettings");
+                return true;
+            }
+            catch
+            {
+                return false;
+            }
+        }
     }
 }

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

@@ -153,21 +153,9 @@ namespace Y.Utils.IOUtils.FileUtils
             if (File.Exists(dstFile) && !overwrite) return -12;//打包后的目标文件已存在
 
             List<string> tempfiles = FileTool.GetAllFile(srcPath);
-            List<FilePackageModel> files = new List<FilePackageModel>();
-            if (ListTool.HasElements(tempfiles))
+            List<FilePackageModel> files = CreateFilePackageModel(tempfiles, srcPath);
+            if (ListTool.HasElements(files))
             {
-                //汇总所有文件
-                tempfiles.ForEach(x =>
-                {
-                    files.Add(new FilePackageModel()
-                    {
-                        Name = Path.GetFileName(x),
-                        Path = DirTool.GetFilePath(x).Substring(srcPath.Count()),
-                        Size = FileTool.Size(x),
-                        MD5 = FileTool.GetMD5(x),
-                    });
-                });
-
                 long allfilesize = files.Sum(x => x.Size);
                 using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
                 {
@@ -264,7 +252,7 @@ namespace Y.Utils.IOUtils.FileUtils
                                         readCount = fsRead.Read(buffer, 0, buffer.Length);
                                         fsWrite.Write(buffer, 0, readCount);
                                         size -= readCount;
-                                    } 
+                                    }
                                     if (size <= FileBuffer)
                                     {
                                         readCount = fsRead.Read(buffer, 0, (int)size);
@@ -307,6 +295,11 @@ namespace Y.Utils.IOUtils.FileUtils
 
             return result;
         }
+        /// <summary>
+        /// 解析打包文件文件列表
+        /// </summary>
+        /// <param name="headdata"></param>
+        /// <returns></returns>
         private static List<FilePackageModel> GetFilePackageModel(byte[] headdata)
         {
             List<FilePackageModel> files = new List<FilePackageModel>();
@@ -383,5 +376,29 @@ namespace Y.Utils.IOUtils.FileUtils
             }
             catch (Exception e) { return null; }
         }
+        /// <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;
+        }
     }
 }