0

操作系统:Windows 7 专业版。

我们正在使用 Roslyn 打开 .csproj 文件以使用以下代码获取其编译详细信息:

using (var workspace = MSBuildWorkspace.Create())
{               

   Project currentProject = workspace.OpenProjectAsync(projectPath).Result; 
   Compilation compilation = currentProject.GetCompilationAsync().Result;
} 

该代码适用于文件路径少于 260 个字符的 .csproj 文件。但是代码在打开文件路径大于 260 个字符的 .csproj 文件时会引发以下异常。

    System.AggregateException: One or more errors occurred. ---> 
    System.IO.PathTooLongException: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
   at System.IO.PathHelper.GetFullPathName()
   at System.IO.Path.LegacyNormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
   at System.IO.Path.GetFullPathInternal(String path)
   at Microsoft.CodeAnalysis.MSBuild.ProjectFile.IsDocumentGenerated(ITaskItem documentItem)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.MakeDocumentFileInfo(String projectDirectory, ITaskItem item)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.<>c__DisplayClass5_0.<CreateProjectFileInfo>b__1(ITaskItem s)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
   at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.CreateProjectFileInfo(CSharpCompilerInputs compilerInputs, BuildInfo buildInfo)
   at Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile.<GetProjectFileInfoAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadProjectAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadProjectInfoAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenProjectAsync>d__23.MoveNext()

但是这个限制必须有一些解决方法。我们正在寻找解决此问题的某些选项。

笔记:

  1. 我们无法将文件路径更改为更短的文件路径。
  2. 我们已经尝试了subst选项,但它仍然不是一个完整的证明选项。
4

0 回答 0