批量修改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;
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
批量修改iconfont的名字,你可以按照以下步骤进行操作: 1. 进入lib目录下,找到flutter_iconfont.dart文件。这个文件包含了所有iconfont的变量定义。 2. 检查每一个变量的名字,并根据需要将其修改为适当的名字。确保变量名字都是英文字母,并避免重复的名字。 3. 你可以打开解压资源包的html文件,对照图标进行变量命名,以确保变量名和图标对应正确。 4. 确认修改后的IconFonts类没有错误,可以进行下一步操作。 5. 在项目工程的pubspec.yaml文件,添加本地库的路径。将路径指向你下载的flutter_iconfont库的实际路径。 6. 在代码导入flutter_iconfont库,然后就可以使用修改后的iconfont名字了。 举例来说,如果你想要使用修改后的Arrow_down图标,你可以这样写代码: ``` import 'package:flutter_iconfont/flutter_iconfont.dart'; return Center(child: Icon(IconFonts.Arrow_down)); ``` 这样,你就可以批量修改iconfont的名字,并在项目使用了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Flutter快速导入大量iconfont资源](https://blog.csdn.net/yingshukun/article/details/90485919)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [修改字体图标类名](https://blog.csdn.net/Luzahngfeng/article/details/126598881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丷丩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值