分支自 SuZhouGuanHong/TaiYuanTaiZhong

PCS
dengjunjie
2023-12-13 113d1d4262d8f9e78a9d92123713c41669ad6c87
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
using System.IO;
using System.Linq;
using WIDESEA_Core.Extensions;
 
namespace WIDESEA_Builder.Utility
{
    public class ProjectPath
    {
        //   private int findCount = 1;
 
        /// <summary>
        /// 获取web父目录所在位置
        /// </summary>
        /// <returns></returns>
        public static DirectoryInfo GetProjectDirectoryInfo()
        {
            return GetProjectDirectoryInfo(new DirectoryInfo("".MapPath()), 1);
        }
        public static string GetProjectFileName(string startsWith)
        {
            var dd = GetProjectDirectoryInfo()?.GetDirectories();
            string fileNames = GetProjectDirectoryInfo()?.GetDirectories()
                     .Where(c => !c.Name.ToLower().EndsWith("_builder")
                              && !c.Name.ToLower().EndsWith("_core")
                              && !c.Name.ToLower().EndsWith("_entity")
                              //&& !c.Name.ToLower().EndsWith("_system")
                              && !c.Name.ToLower().EndsWith("_webapi")
                              && !c.Name.ToLower().EndsWith("_baseserve")
                              && !c.Name.ToLower().EndsWith(".vs")
                     ).Select(x => x.Name).ToList().Serialize();
            return fileNames ?? "''";
        }
        /// <summary>
        /// 获取指定结尾的项目名称 
        /// </summary>
        /// <param name="lastIndexOfName"></param>
        /// <returns></returns>
        public static string GetLastIndexOfDirectoryName(string lastIndexOfName)
        {
            string projectName = GetProjectDirectoryInfo()?.GetDirectories()
                     .Where(c => c.Name.LastIndexOf(lastIndexOfName) != -1).Select(x => x.Name).FirstOrDefault();
            if (string.IsNullOrEmpty(projectName))
            {
                var dllList = new DirectoryInfo("".MapPath()).GetFiles("*.dll", SearchOption.AllDirectories);
                projectName = dllList.Where(x => x.Name.LastIndexOf(lastIndexOfName + ".dll") != -1).FirstOrDefault()?.Name;
                if (!string.IsNullOrEmpty(projectName))
                {
                    projectName = projectName.Replace(".dll", "");
                }
            }
 
            return projectName;
        }
        /// <summary>
        /// 获取项目所在路径
        /// </summary>
        /// <param name="directoryInfo"></param>
        /// <returns></returns>
        private static DirectoryInfo GetProjectDirectoryInfo(DirectoryInfo directoryInfo, int findCount)
        {
            if (directoryInfo == null)
            {
                return null;
            }
            if (directoryInfo.Exists
                && directoryInfo.GetDirectories().Where(x => x.Name.LastIndexOf("_Web") != -1).FirstOrDefault() != null)
            {
                return directoryInfo;
            }
            if (findCount < 7)
            {
                findCount++;
                DirectoryInfo dir = GetProjectDirectoryInfo(directoryInfo.Parent, findCount);
                if (dir != null)
                {
                    return dir;
                }
            }
            return null;
        }
    }
}