黄学彪
2020-07-09 5428935270159bfc42c2934ed7fb1091554fc9a4
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
using System;
using System.Collections.Generic;
 
namespace Shared.Common
{
    public class Global
    {
        /// <summary>
        /// 获取住宅文件夹下面的文件
        /// </summary>
        /// <param name="fileName">File name.</param>
        public static byte[] ReadFileByHomeId(string fileName)
        {
 
            var path = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
            return Shared.IO.FileUtils.ReadFile(path);
        }
 
        public static List<string> FileListByHomeId()
        {
            var list = new List<string> { };
            var path = Config.Instance.FullPath;
            if (!System.IO.Directory.Exists(path)) {
                return new List<string> { };
            }
            var files = System.IO.Directory.GetFiles(path);
            foreach (var file in files)
            {
                var f = file.Substring(path.Length + 1);
                System.Console.WriteLine(f);
                list.Add(f);
            }
            return list;
        }
 
        /// <summary>
        /// 写文件到住宅文件夹目录
        /// </summary>
        /// <param name="fileName">File name.</param>
        /// <param name="bytes">Bytes.</param>
        public static void WriteFileByBytesByHomeId(string fileName, byte[] bytes)
        {
            //如果没有存在住宅目录,先创建
            if (!System.IO.Directory.Exists(Config.Instance.FullPath))
            {
                System.IO.Directory.CreateDirectory(Config.Instance.FullPath);
            }
            var path = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
            Shared.IO.FileUtils.WriteFileByBytes(path, bytes);
        }
        /// <summary>
        /// 删除住宅下面的文件
        /// </summary>
        /// <param name="fileName">File name.</param>
        public static void DeleteFilebyHomeId(string fileName)
        {
            var path = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
            System.IO.File.Delete(path);
        }
 
        /// <summary>
        /// 是否存在当前文件
        /// </summary>
        /// <param name="fileName">File name.</param>
        public static bool IsExistsByHomeId(string fileName)
        {
            var path = System.IO.Path.Combine(Config.Instance.FullPath, fileName);
            return System.IO.File.Exists(path);
        }
 
        /// <summary>
        /// 更新文件名称
        /// </summary>
        /// <param name="oldFilePath">Old file path.</param>
        /// <param name="newFilePath">New file path.</param>
        public static void ReNameFileByHomeId(string oldFilePath, string newFilePath)
        {
            if (oldFilePath == newFilePath)
            {
                return;
            }
 
            var fileInfo = new System.IO.FileInfo(Config.Instance.FullPath + oldFilePath);
            if (fileInfo.Exists)
            {
                DeleteFilebyHomeId(newFilePath);
                fileInfo.MoveTo(Config.Instance.FullPath + newFilePath);
            }
        }
 
        public static void CreateHomeDirectory(string homeId)
        {
            var path= System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Config.Instance.Guid, homeId);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
        }
    }
}