How can I add a new file to .csproj
from command prompt?

- 97,193
- 102
- 206
- 364

- 817
- 3
- 11
- 24
-
4You can edit the .csproj with a text editor. – Eric J. Jan 17 '13 at 15:58
-
1And why would you want to do such a thing? What is the goal? – Shadow The GPT Wizard Jan 17 '13 at 15:59
-
1This is a genuine question, I don't agree that it should have been closed. – Paul Ruane Jan 17 '13 at 15:59
-
With no context and very little information, it should remain closed. – jrummell Jan 17 '13 at 16:02
-
3I want to generate classes (following some kind of pattern) and attach those classes to several .csprojs – Alexander Beninski Jan 17 '13 at 16:04
-
@AlexanderBeninski, that's an awful idea. What happens when you need to change the code in the class? A single class library complied into a DLL seems like a better approach. – James Hill Jan 17 '13 at 16:05
-
1@AlexanderBeninski, take a look at T4 templates to generate your code. – Paul Ruane Jan 17 '13 at 17:20
4 Answers
I don't think there's any tool that will respond to an "add-project" command on the command-line to do this, but I think you could have luck creating a program/script to manipulate the XML content of csproj files directly.
The structure of a csproj file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="...">
<PropertyGroup>
<!-- Properties which affect the build process -->
</PropertyGroup>
<ItemGroup>
<!-- Groups of items to process -->
</ItemGroup>
</Project>
To add a C# code file to a project, you just need to add a Compile
element to an ItemGroup
element:
<Compile Include="{relative path to file}" />
If you use an existing project file as a template, this should be possible with a very simple XSLT.

- 38,949
- 15
- 102
- 166
Here's an easy way you can do it using Linq2Xml. I've included a single and a list based method. Oh, and like Casper said: you'll need to unload the project from VS before your program can edit it. Then just reload it when done.
private static void AddFilesToUnitTestProject(FileInfo[] files, string measureBaseDirPath, string measureDataDirSuffix)
{
var unitTestProjectPath = measureBaseDirPath + _unitTestProjectFile;
var unitTestProjectFile = XDocument.Load(unitTestProjectPath);
var itemGroup = unitTestProjectFile.Nodes()
.OfType<XElement>()
.DescendantNodes()
.OfType<XElement>().First(xy => xy.Name.LocalName == "ItemGroup");
foreach (var fileInfo in files)
{
var xelem = AddProjectContent(measureDataDirSuffix + fileInfo.Name, unitTestProjectFile);
itemGroup.Add(xelem);
}
unitTestProjectFile.Save(unitTestProjectPath);
}
private static void AddFileToUnitTestProject(string pathToAdd, string measureBaseDirPath, string measureDataDir)
{
var unitTestProjectPath = measureBaseDirPath + _unitTestProjectFile;
var unitTestProjectFile = XDocument.Load(unitTestProjectPath);
var itemGroup =
unitTestProjectFile.Nodes()
.OfType<XElement>()
.DescendantNodes()
.OfType<XElement>().First(xy => xy.Name.LocalName == "ItemGroup");
var xelem = AddProjectContent(pathToAdd, unitTestProjectFile);
itemGroup.Add(xelem);
unitTestProjectFile.Save(unitTestProjectPath);
}
private static XElement AddProjectContent(string pathToAdd, XDocument doc)
{
XNamespace rootNamespace = doc.Root.Name.NamespaceName;
var xelem = new XElement(rootNamespace + "Content");
xelem.Add(new XAttribute("Include", pathToAdd));
xelem.Add(new XElement(rootNamespace + "CopyToOutputDirectory", "Always"));
return xelem;
}
- Updated with AddProjectContent method

- 2,662
- 1
- 26
- 50
-
Could you please provide the code for AddProjectContent because your answer should fix my problem.. – Daniel W. Dec 19 '13 at 13:00
-
1Sure no problem. Here ya go: private static XElement AddProjectContent(string pathToAdd, XDocument doc) { XNamespace rootNamespace = doc.Root.Name.NamespaceName; var xelem = new XElement(rootNamespace + "Content"); xelem.Add(new XAttribute("Include", pathToAdd)); xelem.Add(new XElement(rootNamespace + "CopyToOutputDirectory", "Always")); return xelem; } – Dan Csharpster Dec 20 '13 at 16:36
.csproj files can be used as input into the MSBuild command line tool.
See the MSBuild reference:
And for example, this question.
MSBuild — Use the .csproj file or roll your own?
You can simply use a text editor to edit them and do anything MSBuild allows. If you insert custom build actions Visual Studio might warn a developer when the project is loaded that it contains custom actions, depending on what you do.
Unload the project by right clicking it in solution explorer. Edit the project by right clicking. Add f.ex:
<Compile Include="Properties\AssemblyInfo.cs" />

- 2,528
- 1
- 28
- 37
-
I would like to know the reason for the downvote, as this is a working answer to the question asked. – Casper Leon Nielsen Sep 18 '19 at 11:38
-
2I didn't downvote, but the question explicitly says "without visual studio". Your answer doesn't respect that criteria. – Carlos Jimenez Bermudez Jan 03 '21 at 21:19