C# 缓存一个操作的结果,当再次完成时,只扫描更改的结果

C# 缓存一个操作的结果,当再次完成时,只扫描更改的结果,c#,caching,C#,Caching,我正在使用一个我的一位同事编写的工具,该工具扫描目录并向解决方案文件和.csproj文件添加名字对象 internal static void MarkAllProjects() { const string assemblyOutputTypeForLibrary = "library"; Dictionary<string, string> projectEntries = new Dictionary<str

我正在使用一个我的一位同事编写的工具,该工具扫描目录并向解决方案文件和.csproj文件添加名字对象

internal static void MarkAllProjects()
        {
            const string assemblyOutputTypeForLibrary = "library";
            Dictionary<string, string> projectEntries = new Dictionary<string, string>();

            using (var stream = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectMarker.ProjectList.txt")))
            {
                while (stream.EndOfStream == false)
                {
                    string[] projectEntry = stream.ReadLine().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (projectEntry.Count() == 2 && projectEntries.ContainsKey(projectEntry[0]) == false)
                    {
                        projectEntries.Add(projectEntry[0], projectEntry[1]);
                    }
                }
            }

            Parallel.ForEach<KeyValuePair<string, string>>(projectEntries, projectEntry =>
            {
                var files = Directory.GetFiles(sourceDirectoryRoot, projectEntry.Key, SearchOption.AllDirectories);

                Parallel.ForEach<string>(files, file =>
                {
                    XDocument projectFileAsXml = XDocument.Parse(File.ReadAllText(file, Encoding.UTF8));
                    string markerElementIdentifier = string.Empty;
                    string postBuildEventIdentifier = string.Empty;
                    string assemblyNameIdentifier = string.Empty;
                    string propertyGroupIdentifier = string.Empty;
                    string assemblyOutputType = string.Empty;
                    string projectOutputType = string.Empty;

                    if (projectFileAsXml.Root.GetDefaultNamespace() != null && string.IsNullOrWhiteSpace(projectFileAsXml.Root.GetDefaultNamespace().NamespaceName) == false)
                    {
                        markerElementIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ItemDefinitionGroup");
                        postBuildEventIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PostBuildEvent");
                        assemblyNameIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "AssemblyName");
                        propertyGroupIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PropertyGroup");
                        assemblyOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "OutputType");
                        projectOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ProjectTypeGuids");
                    }
                    else
                    {
                        markerElementIdentifier = "ItemDefinitionGroup";
                        postBuildEventIdentifier = "PostBuildEvent";
                        assemblyNameIdentifier = "AssemblyName";
                        propertyGroupIdentifier = "PropertyGroup";
                        assemblyOutputType = "OutputType";
                        projectOutputType = "ProjectTypeGuids";
                    }

                    if (projectFileAsXml.Root.Element(markerElementIdentifier) == null
                        || (projectFileAsXml.Root.Element(markerElementIdentifier) != null
                        && projectFileAsXml.Root.Element(markerElementIdentifier).Attribute("Label") == null))
                    {
                        projectFileAsXml.Root.Add(new XElement(markerElementIdentifier, new XAttribute("Label", projectEntry.Value)));

                        if (projectFileAsXml.Root.Descendants(assemblyOutputType).FirstOrDefault() != null
                            && projectFileAsXml.Root.Descendants(assemblyOutputType).First().Value.ToLower() == assemblyOutputTypeForLibrary
                            && projectFileAsXml.Root.Descendants(assemblyNameIdentifier).FirstOrDefault() != null
                            && projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value.Contains("Test") == false
                            && (projectFileAsXml.Root.Descendants(projectOutputType).FirstOrDefault() == null
                                || projectTypeGuidsToBeExcluded.Exists(item => projectFileAsXml.Root.Descendants(projectOutputType).First().Value.ToUpper().Contains(item)) == false))
                        {

                            string nugetPublishDirectory = string.Concat(targetDirectoryRoot, "\\NuGetPublishings\\", projectEntry.Value, "\\", projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value, "\\lib");
                            Directory.CreateDirectory(nugetPublishDirectory);

                            if (projectFileAsXml.Root.Descendants(postBuildEventIdentifier).FirstOrDefault() == null)
                            {
                                projectFileAsXml.Root.Add(new XElement(propertyGroupIdentifier,
                                    new XElement(postBuildEventIdentifier,
                                        string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\""))));
                            }
                            else
                            {
                                projectFileAsXml.Root.Descendants(postBuildEventIdentifier).First().Value
                                    += Environment.NewLine + string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\"");
                            }
                        }

                        projectFileAsXml.Save(file);
                    }
                });
            });
我正在扫描一个文本文件中包含=“Some category”的文件,以设置.sln文件和.csproj文件中的名字

问题是项目列表中大约有700个项目。我正在相应地更改解决方案和项目的位置,但解决方案依赖的某些项目与解决方案文件没有相同的名称,因此Visual Studio在加载项目文件时会引发一些错误

要解决这个问题,我必须修复文本文件中的名字对象并再次运行该工具,但该工具会扫描所有内容,而不仅仅是更改的文件

是否有一种方法可以缓存结果并只扫描已更改的内容?我认为,当我重新运行该工具来修复错误项目的名字时,它将为我节省大量时间。下面是标记.csproj文件的方法

internal static void MarkAllProjects()
        {
            const string assemblyOutputTypeForLibrary = "library";
            Dictionary<string, string> projectEntries = new Dictionary<string, string>();

            using (var stream = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ProjectMarker.ProjectList.txt")))
            {
                while (stream.EndOfStream == false)
                {
                    string[] projectEntry = stream.ReadLine().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (projectEntry.Count() == 2 && projectEntries.ContainsKey(projectEntry[0]) == false)
                    {
                        projectEntries.Add(projectEntry[0], projectEntry[1]);
                    }
                }
            }

            Parallel.ForEach<KeyValuePair<string, string>>(projectEntries, projectEntry =>
            {
                var files = Directory.GetFiles(sourceDirectoryRoot, projectEntry.Key, SearchOption.AllDirectories);

                Parallel.ForEach<string>(files, file =>
                {
                    XDocument projectFileAsXml = XDocument.Parse(File.ReadAllText(file, Encoding.UTF8));
                    string markerElementIdentifier = string.Empty;
                    string postBuildEventIdentifier = string.Empty;
                    string assemblyNameIdentifier = string.Empty;
                    string propertyGroupIdentifier = string.Empty;
                    string assemblyOutputType = string.Empty;
                    string projectOutputType = string.Empty;

                    if (projectFileAsXml.Root.GetDefaultNamespace() != null && string.IsNullOrWhiteSpace(projectFileAsXml.Root.GetDefaultNamespace().NamespaceName) == false)
                    {
                        markerElementIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ItemDefinitionGroup");
                        postBuildEventIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PostBuildEvent");
                        assemblyNameIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "AssemblyName");
                        propertyGroupIdentifier = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "PropertyGroup");
                        assemblyOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "OutputType");
                        projectOutputType = string.Concat("{", projectFileAsXml.Root.GetDefaultNamespace(), "}", "ProjectTypeGuids");
                    }
                    else
                    {
                        markerElementIdentifier = "ItemDefinitionGroup";
                        postBuildEventIdentifier = "PostBuildEvent";
                        assemblyNameIdentifier = "AssemblyName";
                        propertyGroupIdentifier = "PropertyGroup";
                        assemblyOutputType = "OutputType";
                        projectOutputType = "ProjectTypeGuids";
                    }

                    if (projectFileAsXml.Root.Element(markerElementIdentifier) == null
                        || (projectFileAsXml.Root.Element(markerElementIdentifier) != null
                        && projectFileAsXml.Root.Element(markerElementIdentifier).Attribute("Label") == null))
                    {
                        projectFileAsXml.Root.Add(new XElement(markerElementIdentifier, new XAttribute("Label", projectEntry.Value)));

                        if (projectFileAsXml.Root.Descendants(assemblyOutputType).FirstOrDefault() != null
                            && projectFileAsXml.Root.Descendants(assemblyOutputType).First().Value.ToLower() == assemblyOutputTypeForLibrary
                            && projectFileAsXml.Root.Descendants(assemblyNameIdentifier).FirstOrDefault() != null
                            && projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value.Contains("Test") == false
                            && (projectFileAsXml.Root.Descendants(projectOutputType).FirstOrDefault() == null
                                || projectTypeGuidsToBeExcluded.Exists(item => projectFileAsXml.Root.Descendants(projectOutputType).First().Value.ToUpper().Contains(item)) == false))
                        {

                            string nugetPublishDirectory = string.Concat(targetDirectoryRoot, "\\NuGetPublishings\\", projectEntry.Value, "\\", projectFileAsXml.Root.Descendants(assemblyNameIdentifier).First().Value, "\\lib");
                            Directory.CreateDirectory(nugetPublishDirectory);

                            if (projectFileAsXml.Root.Descendants(postBuildEventIdentifier).FirstOrDefault() == null)
                            {
                                projectFileAsXml.Root.Add(new XElement(propertyGroupIdentifier,
                                    new XElement(postBuildEventIdentifier,
                                        string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\""))));
                            }
                            else
                            {
                                projectFileAsXml.Root.Descendants(postBuildEventIdentifier).First().Value
                                    += Environment.NewLine + string.Concat("copy \"$(TargetDir)$(TargetFileName)\" ", "\"$(SolutionDir)..\\NuGetPublishings\\", projectEntry.Value, "\\$(ProjectName)\\lib\\$(TargetFileName)\"");
                            }
                        }

                        projectFileAsXml.Save(file);
                    }
                });
            });
内部静态void MarkAllProjects()
{
常量字符串assemblyOutputTypeForLibrary=“library”;
Dictionary projectEntries=新建字典();
使用(var stream=new System.IO.StreamReader(System.Reflection.Assembly.getExecutionGassembly().GetManifestResourceStream(“ProjectMarker.ProjectList.txt”))
{
while(stream.EndOfStream==false)
{
string[]projectEntry=stream.ReadLine().Split(新字符[]{'='},StringSplitOptions.RemoveEmptyEntries);
if(projectEntry.Count()==2&&projectEntries.ContainsKey(projectEntry[0])==false)
{
添加(projectEntry[0],projectEntry[1]);
}
}
}
Parallel.ForEach(projectEntries,projectEntry=>
{
var files=Directory.GetFiles(sourceDirectoryRoot、projectEntry.Key、SearchOption.AllDirectories);
Parallel.ForEach(文件,文件=>
{
XDocument projectFileAsXml=XDocument.Parse(File.ReadAllText(File,Encoding.UTF8));
string-markerementidentifier=string.Empty;
string postBuildEventIdentifier=string.Empty;
string assemblyNameIdentifier=string.Empty;
string propertyGroupIdentifier=string.Empty;
string assemblyOutputType=string.Empty;
string projectOutputType=string.Empty;
if(projectFileAsXml.Root.GetDefaultNamespace()!=null&&string.IsNullOrWhiteSpace(projectFileAsXml.Root.GetDefaultNamespace().NamespaceName)==false)
{
markerElementIdentifier=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“ItemDefinitionGroup”);
postBuildEventIdentifier=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“PostBuildEvent”);
assemblyNameIdentifier=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“AssemblyName”);
propertyGroupIdentifier=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“PropertyGroup”);
assemblyOutputType=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“OutputType”);
projectOutputType=string.Concat(“{”,projectFileAsXml.Root.GetDefaultNamespace(),“}”,“ProjectTypeGuids”);
}
其他的
{
markerElementIdentifier=“ItemDefinitionGroup”;
postBuildEventIdentifier=“PostBuildEvent”;
assemblyNameIdentifier=“AssemblyName”;
propertyGroupIdentifier=“PropertyGroup”;
assemblyOutputType=“OutputType”;
projectOutputType=“ProjectTypeGuids”;
}
if(projectFileAsXml.Root.Element(markerElementIdentifier)==null
||(projectFileAsXml.Root.Element(markerElementIdentifier)!=null
&&projectFileAsXml.Root.Element(markerementidentifier.Attribute(“Label”)==null))
{
projectFileAsXml.Root.Add(新的XElement(markerementIdentifier,新的XAttribute(“Label”,projectEntry.Value));
if(projectFileAsXml.Root.subjections(assemblyOutputType).FirstOrDefault()!=null
&&projectFileAsXml.Root.Subjections(assemblyOutputType).First().Value.ToLower()==assemblyOutputTypeForLibrary
&&projectFileAsXml.Root.subjects(assemblyNameIdentifier).FirstOrDefault()!=null
&&projectFileAsXml.Root.subjects(assemblyNameIdentifier.First().Value.Contains(“Test”)==false
&&(projectFileAsXml.Root.subjects(projectOutputType).FirstOrDefault()==null
||projectTypeGuidsToBeExcluded.Exists(项=>projectFileAsXml.Root.substands(projectOutputType.First().Value.ToUpper().Contains(项))==false))
{
string nugetPublishDirectory=string.Concat(targetDirectoryRoot,“\\NuGetPublishings\\”,projectEntry.Value,“\\”,projectFileAsXml.Root.Subscriptions(assemblyNameIdentifier.First().Value,“\\lib”);
CreateDirectory(nugetPublishDirectory);
if(projectFileAsXml.Root.subjections(postBuildEventIdentifier).FirstOrDefault()==null)
{
projectFileAsXml.Root.Add(新的XElement(propertyGroupIdentifier,
新希莱姆