如何设置几个可视化C#项目的输出路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/621461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the output path of several visual C# projects
提问by Paul Dixon
I have a solution that contains several c# projects and I would like to be able to set the output path and other properties on all the projects together in a single place. Property Sheets (vsprops) do not seem to be able available for C# projects and the $(SolutionDir) variable is ignored. Are there any other methods to set properties across several C# projects?
我有一个包含多个 c# 项目的解决方案,我希望能够在一个地方设置所有项目的输出路径和其他属性。属性表 (vsprops) 似乎无法用于 C# 项目,并且 $(SolutionDir) 变量被忽略。是否还有其他方法可以跨多个 C# 项目设置属性?
UpdateBy Following the information in the answer by Bas Bossink I was able to set the output path of several projects by creating a common csproj and importing it into the individual project. A few other points:
通过遵循 Bas Bossink 的回答中的信息进行更新,我能够通过创建一个通用的 csproj 并将其导入到单个项目中来设置多个项目的输出路径。其他几点:
- When building in Visual Studio if changes are made to the common project it is necessary to touch/reload any projects that reference it for the changes to be picked up.
- Any properties which are also set in a individual project will override the common properties.
- Setting $(SolutionDir) as the output path via the Visual Studio UI does not work as expected because the value is treated as a string literal rather than getting expanded. However, Setting $(SolutionDir) directly into the csproj file with a text editor works as expected.
- 在 Visual Studio 中构建时,如果对公共项目进行了更改,则有必要触摸/重新加载引用它的任何项目以获取更改。
- 在单个项目中设置的任何属性都将覆盖公共属性。
- 通过 Visual Studio UI 将 $(SolutionDir) 设置为输出路径无法按预期工作,因为该值被视为字符串文字而不是被扩展。但是,使用文本编辑器将 $(SolutionDir) 直接设置到 csproj 文件中可以按预期工作。
采纳答案by Bas Bossink
A csproj file is already an msbuild file, this means that csproj files can also use an import element as described here. The import element is exactly what you require. You could create a Common.proj that contains something like:
csproj 文件已经是 msbuild 文件,这意味着 csproj 文件也可以使用此处所述的导入元素。import 元素正是您所需要的。您可以创建一个包含以下内容的 Common.proj:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5"xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<OutputPath>$(SolutionDir)output</OutputPath>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
You can import this Common.proj in each of your csprojs, for instance like so:
您可以在每个 csprojs 中导入此 Common.proj,例如:
<Import Project="..\Common.proj" />
The import statement should precede any tasks that depend on the properties defined in Common.proj
import 语句应该在依赖于 Common.proj 中定义的属性的任何任务之前
I hope this helps. I can't confirm your problems with the $(SolutionDir) variable I've used it many times. I do know however that this variable does not get set when you run an msbuild command via the commandline on a specific project that is contained in a solution. It will be set when you build your solution in Visual Studio.
我希望这有帮助。我无法确认您使用 $(SolutionDir) 变量的问题,我已经多次使用它。但是,我知道当您通过命令行在解决方案中包含的特定项目上运行 msbuild 命令时,不会设置此变量。它将在您在 Visual Studio 中构建解决方案时设置。
回答by abhilash
I would suggest you to use a build tool such as MSBuildor NAntwhich would give you more flexibility on your builds. Basically the idea is to kick off a build using (in most cases) a single configurable build file.
我建议您使用诸如MSBuild或NAnt 之类的构建工具,这将为您的构建提供更大的灵活性。基本上这个想法是使用(在大多数情况下)单个可配置的构建文件来启动构建。
I would personally recommend NAnt.
我个人会推荐 NAnt。
You could find an awesome tutorial on NAnt on JP Boodhoo's blog here
你可以找到关于JP布杜的博客上恶性一个真棒教程这里
回答by marc_s
Unfortunately, these bits of information such as output path are all stored inside the individual *.csproj files. If you want to batch-update a whole bunch of those, you'll have to revert to some kind of a text-updating tool or create a script to touch each of those files.
不幸的是,这些诸如输出路径之类的信息都存储在单独的 *.csproj 文件中。如果您想批量更新其中的一大堆文件,您将不得不使用某种文本更新工具或创建一个脚本来处理这些文件中的每一个。
For things like this (apply changes to a bunch of text files at once) I personally use WildEditby Helios Software - works like a charm and it's reasonably priced.
对于这样的事情(一次对一堆文本文件应用更改),我个人使用Helios Software 的WildEdit- 效果很好,而且价格合理。
But I'm sure there are tons of free alternatives out there, too.
但我相信那里也有很多免费的替代品。
回答by C Johnson
Set the $(OutputPath)
property in a common property sheet. Then delete that entry in all the project files you want to it to affect. Then import that property sheet into all your projects.
$(OutputPath)
在公共属性表中设置属性。然后在您希望它影响的所有项目文件中删除该条目。然后将该属性表导入到您的所有项目中。
For hundreds of projects that can be very tedious. Which is why I wrote a tool to help with this:
对于数百个可能非常乏味的项目。这就是为什么我写了一个工具来帮助解决这个问题: