wei
2020-12-09 16deafb8aca7709877907ba938ea31407f6d2834
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.IO;
using HDL_ON.Entity;
 
namespace HDL_ON.Common
{
    public class FileUtlis
    {
        static Common.FileUtlis _temp;
        public static Common.FileUtlis Files
        {
            get
            {
                if (_temp == null)
                {
                    _temp = new Common.FileUtlis();
                }
                return _temp;
            }
        }
        /// <summary>
        /// 根目录
        /// </summary>
        string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/";
        /// <summary>
        /// 住宅文件夹路径
        /// </summary>
        string regionPath;
        string RegionPath
        {
            get
            {
                if (string.IsNullOrEmpty(regionPath))
                {
                    regionPath = Path.Combine(RootPath, DB_ResidenceData.residenceData.CurReginID);
                    if (!Directory.Exists(regionPath))
                    {
                        Directory.CreateDirectory(regionPath);
                    }
                }
                return regionPath + "/";
            }
        }
        /// <summary>
        /// 用户信息路径
        /// </summary>
        string userInfoPath
        {
            get
            {
                return "UserInfo_File";
            }
        }
 
        /// <summary>
        /// 遍历文件
        /// </summary>
        public List<string> ReadFiles()
        {
            List<string> listFiles = new List<string>();
            DirectoryInfo TheFolder = new DirectoryInfo(RegionPath);
            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                listFiles.Add(NextFile.Name);
            }
            return listFiles;
        }
 
        /// <summary>
        /// Deletes the file.
        /// </summary>
        /// <param name="fileName">File name.</param>
        public void DeleteFile(string fileName)
        {
            File.Delete(Path.Combine(RegionPath, fileName));
            MainPage.Log("删除文件名为:" + fileName);
        }
 
        /// <summary>
        /// Deletes all file.
        /// </summary>
        public void DeleteAllFile()
        {
            List<string> filesList = ReadFiles();
            for (int j = 0; j < filesList.Count; j++)
            {
                var f = filesList[j];
                DeleteFile(f);
            }
        }
 
        /// <summary>
        /// Writes the file by bytes.
        /// </summary>
        /// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns>
        /// <param name="fileName">File name.</param>
        /// <param name="bytes">Bytes.</param>
        public bool WriteFileByBytes(string fileName, byte[] bytes)
        {
            if (fileName == null || (fileName = fileName.Trim()) == (""))
            {
                return false;
            }
 
            FileStream fs = null;
 
            try
            {
                fs = new FileStream(Path.Combine(RegionPath, fileName), FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                MainPage.Log("SaveFile:" + fileName);
                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());
                }
            }
        }
 
        public byte[] ReadFile(string fileName)
        {
            FileStream fs = null;
            try
            {
                if (File.Exists(Path.Combine(RegionPath, fileName)))
                {
                    fs = new FileStream(Path.Combine(RegionPath, 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
                {
 
                }
            }
        }
    }
}