Browse Source

修改Http下载文件临时文件名,添加Guid短格式

yuzhengyang 8 years ago
parent
commit
c04ee8b4e4

BIN
Fork.Net/.vs/Fork.Net/v15/sqlite3/storage.ide


+ 4 - 23
Fork.Net/Fork.Net.Tools/Y.Utils/AppUtils/StartupTool.cs

@@ -15,30 +15,11 @@ namespace Y.Utils.AppUtils
     /// </summary>
     /// </summary>
     public class StartupTool
     public class StartupTool
     {
     {
-        [Obsolete]
-        public static string RegeditRunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
 
 
-        public static string regAll = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
-        public static string regCurrent = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
-        public static string commonStartup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
-        public static string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
-        [Obsolete]
-        public static bool Regedit(string appName, string appFile, bool start = true)
-        {
-            if (start)
-            {
-                //添加启动注册表
-                if (RegisterTool.Write(RegeditRunKey, appName, appFile))
-                    return true;
-            }
-            else
-            {
-                //删除启动注册表
-                if (RegisterTool.Delete(RegeditRunKey, appName))
-                    return true;
-            }
-            return false;
-        }
+        private static string regAll = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
+        private static string regCurrent = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
+        private static string commonStartup = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
+        private static string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
         public static bool Register(string name, string file, bool start = true, bool allUser = true)
         public static bool Register(string name, string file, bool start = true, bool allUser = true)
         {
         {
             if (start)
             if (start)

+ 41 - 0
Fork.Net/Fork.Net.Tools/Y.Utils/DataUtils/GuidUtils/GuidTool.cs

@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Y.Utils.DataUtils.GuidUtils
+{
+    public class GuidTool
+    {
+        /// <summary>
+        /// //短GUID:e0a953c3ee6040eaa9fae2b667060e09
+        /// </summary>
+        /// <returns></returns>
+        public static string Short() { return Guid.NewGuid().ToString("N"); }
+        /// <summary>
+        /// //转换短GUID:e0a953c3ee6040eaa9fae2b667060e09
+        /// </summary>
+        /// <returns></returns>
+        public static string Short(Guid guid) { return guid.ToString("N"); }
+        /// <summary>
+        /// //转换短GUID:e0a953c3ee6040eaa9fae2b667060e09
+        /// </summary>
+        /// <returns></returns>
+        public static string Short(string guid) { return guid.Replace("-", ""); }
+
+        //public static string Format()
+        //{
+        //    var uuid = Guid.NewGuid().ToString(); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
+
+        //    var uuidN =
+
+        //    var uuidD = Guid.NewGuid().ToString("D"); // 9af7f46a-ea52-4aa3-b8c3-9fd484c2af12  
+
+        //    var uuidB = Guid.NewGuid().ToString("B"); // {734fd453-a4f8-4c5d-9c98-3fe2d7079760}  
+
+        //    var uuidP = Guid.NewGuid().ToString("P"); //  (ade24d16-db0f-40af-8794-1e08e2040df3)  
+
+        //    var uuidX = Guid.NewGuid().ToString("X"); // {0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}}  
+        //}
+    }
+}

+ 10 - 9
Fork.Net/Fork.Net.Tools/Y.Utils/NetUtils/HttpUtils/HttpTool.cs

@@ -9,8 +9,10 @@ using System.IO;
 using System.Net;
 using System.Net;
 using System.Text;
 using System.Text;
 using System.Web;
 using System.Web;
+using Y.Utils.DataUtils.GuidUtils;
 using Y.Utils.DataUtils.StringUtils;
 using Y.Utils.DataUtils.StringUtils;
 using Y.Utils.DelegateUtils;
 using Y.Utils.DelegateUtils;
+using Y.Utils.IOUtils.PathUtils;
 
 
 namespace Y.Utils.NetUtils.HttpUtils
 namespace Y.Utils.NetUtils.HttpUtils
 {
 {
@@ -370,15 +372,14 @@ namespace Y.Utils.NetUtils.HttpUtils
         /// <returns></returns>
         /// <returns></returns>
         public static bool Download(string url, string file, ProgressDelegate.ProgressHandler progress = null, object sender = null)
         public static bool Download(string url, string file, ProgressDelegate.ProgressHandler progress = null, object sender = null)
         {
         {
-            string tempPath = Path.GetDirectoryName(file) + @"\temp";
-            Directory.CreateDirectory(tempPath);  //创建临时文件目录
-            string tempFile = tempPath + @"\" + Path.GetFileName(file) + ".temp"; //临时文件
-            if (File.Exists(tempFile))
-            {
-                File.Delete(tempFile);    //存在则删除
-            }
             try
             try
             {
             {
+                string path = Path.GetDirectoryName(file);
+                DirTool.Create(path);  //创建文件目录
+
+                string tempFile = DirTool.Combine(path, GuidTool.Short() + ".temp"); //临时文件
+                if (File.Exists(tempFile)) File.Delete(tempFile);    //存在则删除
+
                 FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                 FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                 // 设置参数
                 // 设置参数
                 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
@@ -400,8 +401,8 @@ namespace Y.Utils.NetUtils.HttpUtils
                 //stream.Close();
                 //stream.Close();
                 fs.Close();
                 fs.Close();
                 responseStream.Close();
                 responseStream.Close();
-                File.Delete(file);
-                File.Move(tempFile, file);
+                File.Delete(file);//删除原始文件
+                File.Move(tempFile, file);//下载的临时文件重命名
                 return true;
                 return true;
             }
             }
             catch (Exception ex)
             catch (Exception ex)

+ 2 - 0
Fork.Net/Fork.Net.Tools/Y.Utils/Y.Utils.csproj

@@ -54,6 +54,7 @@
     <Compile Include="AppUtils\AppSettleTool.cs" />
     <Compile Include="AppUtils\AppSettleTool.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="DataUtils\DateTimeUtils\DateTool.cs" />
     <Compile Include="DataUtils\DateTimeUtils\DateTool.cs" />
+    <Compile Include="DataUtils\GuidUtils\GuidTool.cs" />
     <Compile Include="IOUtils\ImageUtils\ImageSpliter.cs" />
     <Compile Include="IOUtils\ImageUtils\ImageSpliter.cs" />
     <Compile Include="UpdateUtils\AppUpdateInfo.cs" />
     <Compile Include="UpdateUtils\AppUpdateInfo.cs" />
     <Compile Include="UpdateUtils\AppUpdateTool.cs" />
     <Compile Include="UpdateUtils\AppUpdateTool.cs" />
@@ -168,6 +169,7 @@
   <ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
     <None Include="packages.config" />
   </ItemGroup>
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.
        Other similar extension points exist, see Microsoft.Common.targets.