Visual Studio 2019项目构建错误:找不到指定路径片段,求解决方法
解决Visual Studio 2019中.NET Framework 4.8项目发布时的文件复制错误
错误信息
1>obj\\Debug\\net48\\Package\\PackageTmp. 1>C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Microsoft\\VisualStudio\\v16.0\\Web\\Microsoft.Web.Publishing.targets(3000,5): error : Copying file obj\\Debug\\net48\\NuGet\\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\\Glass.Mapper.Sc.100\\5.8.173\\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config to obj\\Debug\\net48\\Package\\PackageTmp\\obj\\Debug\\net48\\NuGet\\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\\Glass.Mapper.Sc.100\\5.8.173\\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config failed. Could not find a part of the path 'obj\\Debug\\net48\\Package\\PackageTmp\\obj\\Debug\\net48\\NuGet\\DD3A9BE7C0EDA549CD5B7B690E0621F0E2C932EC\\Glass.Mapper.Sc.100\\5.8.173\\App_Config/Include/Glass/Glass.Mapper.Sc.Start.config'. 1>Done building project "some.project.ORM.csproj" -- FAILED.
已尝试的无效操作
- 删除文件系统及解决方案中的
obj\Debug文件夹 - 卸载项目检查
.csproj文件(未发现异常) - 卸载并重装NuGet包
Glass.Mapper.Sc.100 - 确认源文件路径存在
项目配置(.csproj)
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net48</TargetFramework> <RootNamespace>some.project.ORM</RootNamespace> <AssemblyName>some.project.ORM</AssemblyName> <PublishTargetType>website</PublishTargetType> </PropertyGroup> <Import Project="$(SolutionDir)\\_Build\\props\\_PublishTargetType.props" /> <ItemGroup> <PackageReference Include="Glass.Mapper.Sc.100" Version="5.8.173" /> </ItemGroup> <ItemGroup> <Content Update="App_Config\\Include\\Foundation\\Glass\\Glass.Mapper.Sc.Start.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Project>
解决方案
1. 检查自定义发布配置文件
查看$(SolutionDir)_Build\props\_PublishTargetType.props中的配置,确认是否存在错误的文件包含规则(例如误将obj\**目录加入发布内容集合),或是修改了CopyToPublishDirectory等相关属性导致路径嵌套。
2. 排除NuGet缓存目录的发布
在.csproj中添加排除规则,阻止obj目录下的文件被纳入发布范围:
<ItemGroup> <Content Remove="obj\**" /> <None Include="obj\**" /> </ItemGroup>
3. 调整本地Content文件的发布路径
本地App_Config\Include\Foundation\Glass\Glass.Mapper.Sc.Start.config与NuGet包中的同名文件可能导致MSBuild混淆源路径,可指定明确的输出路径避免冲突:
<Content Update="App_Config\Include\Foundation\Glass\Glass.Mapper.Sc.Start.config"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <TargetPath>App_Config/Include/Glass/Glass.Mapper.Sc.Start.config</TargetPath> </Content>
若本地文件用于覆盖NuGet包文件,可直接排除NuGet包中的内容文件:
<ItemGroup> <PackageReference Include="Glass.Mapper.Sc.100" Version="5.8.173"> <ExcludeAssets>contentFiles</ExcludeAssets> </PackageReference> </ItemGroup>
4. 自定义MSBuild发布目标过滤无效文件
通过自定义目标移除发布集合中的obj目录文件:
<PropertyGroup> <CopyAllFilesToSingleFolderForPackageDependsOn> $(CopyAllFilesToSingleFolderForPackageDependsOn); CustomExcludeObjFiles </CopyAllFilesToSingleFolderForPackageDependsOn> </PropertyGroup> <Target Name="CustomExcludeObjFiles"> <ItemGroup> <FilesForPackagingFromProject Remove="@(FilesForPackagingFromProject)" Condition="'%(FullPath)' Contains 'obj\'" /> </ItemGroup> </Target>
5. 彻底清理并重建
- 关闭Visual Studio
- 删除解决方案下的
.vs文件夹、所有bin和obj目录 - 重新打开Visual Studio,右键解决方案选择「还原NuGet包」
- 重新生成并发布项目
内容的提问来源于stack exchange,提问作者Paul T. Rykiel
相关产品推荐
相关产品推荐

