批量修改vs工程中的引用错误

本文介绍通过代码批量修改vs工程中的引用错误(引用的动态库不存在),尤其对大量工程的时候有效。

思路:匹配工程文件csproj中的引用文件名称与实际路径,修改为相对路径。

class Program
    {
        static void Main(string[] args)
        {
            modifyCsproj();
        }

        private static void modifyCsproj()
        {
            List<string> files = new List<string>();
            //实际dll存放路径
            DirectoryInfo dirInfo = new DirectoryInfo(@"..\lib");
            
            foreach (var file in dirInfo.GetFiles())
            {
                files.Add(file.FullName);
            }

            List<string> csprojs = new List<string>();
            //工程所在的文件夹或者父文件夹
            string path = @"..\Code";
            getAllCsProj(path, ref csprojs);
            //遍历所有文件夹
            foreach (var xmlfile in csprojs)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlfile);
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("ab", "http://schemas.microsoft.com/developer/msbuild/2003");
                XmlNodeList nodes = doc.SelectNodes("//ab:HintPath", nsmgr); 
                foreach (XmlNode n in nodes)
                {
                    string dllPath = n.InnerText;
                    if (!dllPath.EndsWith("dll")) continue;
                    FileInfo dllFile = new FileInfo(dllPath);
                    foreach (var f in files)
                    {
                        FileInfo dllFileNew = new FileInfo(f);
                        if (dllFile.Name == dllFileNew.Name)
                        {
                            //替换为相对路径
                            n.InnerText = GetRelativePath(xmlfile,dllFileNew.FullName);
                            continue;
                        }
                    }
                }
                doc.Save(xmlfile);
            }
        }
        //获取指定路径下的所有csproj文件的全路径
        //递归获取
        private static void getAllCsProj(string path,ref List<string> csprojs)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            foreach (var file in dirInfo.GetFiles())
            {
                if (file.FullName.EndsWith(".csproj")) csprojs.Add(file.FullName);
            }
            foreach (var dir in dirInfo.GetDirectories())
            {
                getAllCsProj(dir.FullName,ref csprojs);
            }
        }
        //获取path2相对path1的相对路径
        //入参path1和path2均为绝对路径
        public static string GetRelativePath(string path1, string path2)
        {
            string[] path1Array = path1.Split('\\');
            string[] path2Array = path2.Split('\\');
            //
            int s = path1Array.Length >= path2Array.Length ? path2Array.Length : path1Array.Length;
            //两个目录最底层的共用目录索引
            int closestRootIndex = -1;
            for (int i = 0; i < s; i++)
            {
                if (path1Array[i] == path2Array[i])
                {
                    closestRootIndex = i;
                }
                else
                {
                    break;
                }
            }
            //由path1计算 ‘../'部分
            string path1Depth = "";
            for (int i = 0; i < path1Array.Length; i++)
            {
                if (i > closestRootIndex + 1)
                {
                    path1Depth += "../";
                }
            }
            //由path2计算 ‘../'后面的目录
            string path2Depth = "";
            for (int i = closestRootIndex + 1; i < path2Array.Length; i++)
            {
                path2Depth += "/" + path2Array[i];
            }
            path2Depth = path2Depth.Substring(1);
            return path1Depth + path2Depth;
        }
    }

猜你喜欢

转载自blog.csdn.net/eqmaster/article/details/86487833