From eda3fb873e59544ff36301b51e05aef64f87b0f9 Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期五, 27 八月 2021 13:21:21 +0800
Subject: [PATCH] Merge branch 'newBranch1' of http://172.16.1.23:6688/r/~wxr/HDL_APP_Project into newBranch1

---
 HDL_ON/Common/FileUtlis.cs |  241 +++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 229 insertions(+), 12 deletions(-)

diff --git a/HDL_ON/Common/FileUtlis.cs b/HDL_ON/Common/FileUtlis.cs
index 013e023..d0a8977 100644
--- a/HDL_ON/Common/FileUtlis.cs
+++ b/HDL_ON/Common/FileUtlis.cs
@@ -1,6 +1,7 @@
 锘縰sing System;
 using System.Collections.Generic;
 using System.IO;
+using System.Net;
 using HDL_ON.Entity;
 
 namespace HDL_ON.Common
@@ -23,6 +24,23 @@
         /// 鏍圭洰褰�
         /// </summary>
         string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/";
+
+        string accountPath;
+        string AccountPath
+        {
+            get
+            {
+                if (string.IsNullOrEmpty(accountPath) || !accountPath.Contains(UserInfo.Current.ID))
+                {
+                    accountPath = Path.Combine(RootPath, UserInfo.Current.ID);
+                    if (!Directory.Exists(accountPath))
+                    {
+                        Directory.CreateDirectory(accountPath);
+                    }
+                }
+                return accountPath + "/";
+            }
+        }
         /// <summary>
         /// 浣忓畢鏂囦欢澶硅矾寰�
         /// </summary>
@@ -31,9 +49,13 @@
         {
             get
             {
-                if (string.IsNullOrEmpty(regionPath))
+                if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null)
                 {
-                    regionPath = Path.Combine(RootPath, DB_ResidenceData.residenceData.CurReginID);
+                    return AccountPath;
+                }
+                if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(DB_ResidenceData.Instance.CurrentRegion.id))
+                {
+                    regionPath = Path.Combine(AccountPath, DB_ResidenceData.Instance.CurrentRegion.id);
                     if (!Directory.Exists(regionPath))
                     {
                         Directory.CreateDirectory(regionPath);
@@ -42,16 +64,9 @@
                 return regionPath + "/";
             }
         }
-        /// <summary>
-        /// 鐢ㄦ埛淇℃伅璺緞
-        /// </summary>
-        string userInfoPath
-        {
-            get
-            {
-                return "UserInfo_File";
-            }
-        }
+
+        //public List<>
+
 
         /// <summary>
         /// 閬嶅巻鏂囦欢
@@ -108,6 +123,90 @@
             try
             {
                 fs = new FileStream(Path.Combine(RegionPath, fileName), FileMode.Create, FileAccess.Write);
+                fs.Write(bytes, 0, bytes.Length);
+                fs.Flush();
+                MainPage.Log($"SaveFile:{fileName}");//Path:{RegionPath}
+                return true;
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log("FileUtiles Code 113:" + ex.ToString());
+                return false;
+            }
+            finally
+            {
+                try
+                {
+                    if (fs != null)
+                    {
+                        fs.Close();
+                    }
+                }
+                catch (Exception ex)
+                {
+                    MainPage.Log("FileUtils Code 121 :" + ex.ToString());
+                }
+            }
+        }
+        /// <summary>
+        /// 璇诲彇userInfo鏂囦欢
+        /// </summary>
+        /// <returns></returns>
+        public byte[] ReadAppConfig()
+        {
+            var fileName = "OnAppConfig";
+            FileStream fs = null;
+            try
+            {
+                if (File.Exists(Path.Combine(RootPath, fileName)))
+                {
+                    fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
+                }
+                else if (File.Exists(fileName))
+                {
+                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
+                }
+                else
+                {
+                    return new byte[0];
+                }
+                byte[] bytes = new byte[fs.Length];
+                fs.Read(bytes, 0, bytes.Length);
+                return bytes;
+            }
+            catch
+            {
+                return new byte[0];
+            }
+            finally
+            {
+                try
+                {
+                    if (fs != null)
+                    {
+                        fs.Close();
+                    }
+                }
+                catch
+                {
+
+                }
+            }
+
+        }
+        /// <summary>
+        /// 淇濆瓨userInfo
+        /// </summary>
+        /// <param name="bytes"></param>
+        /// <returns></returns>
+        public bool WirteAppConfig(byte[] bytes)
+        {
+            var fileName = "OnAppConfig";
+            FileStream fs = null;
+
+            try
+            {
+                fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
                 fs.Write(bytes, 0, bytes.Length);
                 fs.Flush();
                 MainPage.Log("SaveFile:" + fileName);
@@ -174,5 +273,123 @@
                 }
             }
         }
+
+        // 璇诲彇鎸囧畾璺緞鏂囦欢鍐呭
+        public byte[] ReadFileForPath(string path)
+        {
+            FileStream fs = null;
+            try
+            {
+                if (File.Exists(path))
+                {
+                    fs = new FileStream(path, FileMode.Open, FileAccess.Read);
+                }
+                else
+                {
+                    return new byte[0];
+                }
+                byte[] bytes = new byte[fs.Length];
+                fs.Read(bytes, 0, bytes.Length);
+                return bytes;
+            }
+            catch
+            {
+                return new byte[0];
+            }
+            finally
+            {
+                try
+                {
+                    if (fs != null)
+                    {
+                        fs.Close();
+                    }
+                }
+                catch
+                {
+
+                }
+            }
+        }
+
+        /// <summary>
+        /// 涓嬭浇缃戠粶鍥剧墖
+        /// </summary>
+        /// <param name="fileName"></param>
+        /// <param name="url"></param>
+        public void DownLoadImage(string fileName, string url, Action action)
+        {
+            if (!File.Exists(fileName))
+            {
+                System.Threading.Tasks.Task.Run(() =>
+                {
+                    FileStream fs = null;
+                    try
+                    {
+                        MyWebClient webClient = new MyWebClient();
+                        byte[] recevieBytes = webClient.DownloadData(new Uri(url));
+                        fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
+                        //fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
+                        fs.Write(recevieBytes, 0, recevieBytes.Length);
+                        fs.Flush();
+                    }
+                    catch (Exception ex)
+                    {
+                        MainPage.Log($"down image : {ex.Message}");
+                    }
+                    finally
+                    {
+                        if (fs != null)
+                        {
+                            fs.Close();
+                            action?.Invoke();
+                        }
+                    }
+                });
+            }
+            else
+            {
+                action?.Invoke();
+            }
+        }
+
     }
+
+
+    public class MyWebClient : WebClient
+    {
+        int _timeout = 10000;
+
+        /// <summary>
+        /// 瓒呮椂鏃堕棿(姣)
+        /// </summary>
+        public int Timeout
+        {
+            get
+            {
+                return _timeout;
+            }
+            set
+            {
+                _timeout = value;
+            }
+        }
+        public MyWebClient()
+        {
+
+        }
+
+        public MyWebClient(int timeout)
+        {
+            this._timeout = timeout;
+        }
+
+        protected override WebRequest GetWebRequest(Uri address)
+        {
+            var result = base.GetWebRequest(address);
+            result.Timeout = this._timeout;
+            return result;
+        }
+    }
+
 }

--
Gitblit v1.8.0