Groups | Blog | Home
all groups > dotnet faqs > march 2006 >

dotnet faqs : How to generate the .Net project file ".*proj" dynamically in .Net 2.0 ???


SunYour
3/16/2006 4:47:30 AM
Hello,

Take a look at the Microsoft.Build namespace!
There you will find a lot (!!!) of nice features. I've used it to
traverse and insert nodes to the project file. It works and it is easy
to use!!

Look at, for examle, Microsoft.Build.BuildEngine in msdn

Regards
/Magnus
Kalpesh
3/16/2006 5:05:17 AM
Hi,

I think the usual csc/vbc compiler will be enough, if it is a simple
c#/vb file
It doesnt require project to compile the class into dll

Kalpesh
SunYour
3/16/2006 6:21:27 AM
Hello,

You can create a new project file as well, not only modifying them.
If you do not explicitly want to have a project file, use the csc/vbc
compiler.

Example from msdn:

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Build.BuildEngine;

namespace AddNewItem
{
class Program
{
/// <summary>
/// This code demonstrates the use of the following methods:
/// Engine constructor
/// Project constructor
/// Project.LoadFromXml
/// Project.Xml
/// BuildItemGroupCollection.GetEnumerator
/// BuildItemGroup.GetEnumerator
/// BuildItem.Name (get)
/// BuildItem.Include (set)
/// BuildItem.GetMetadata
/// BuildItem.SetMetadata
/// BuildItemGroup.RemoveItem
/// BuildItemGroup.AddNewItem
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
// Create a new Engine object.
Engine engine = new Engine(Environment.CurrentDirectory);

// Create a new Project object.
Project project = new Project(engine);

// Load the project with the following XML, which contains
// two ItemGroups.
project.LoadXml(@"
<Project
xmlns='http://schemas.microsoft.com/developer/msbuild/2003'>

<ItemGroup>
<Compile Include='Program.cs'/>
<Compile Include='Class1.cs'/>
<RemoveThisItemPlease Include='readme.txt'/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include='Strings.resx'>

<LogicalName>Strings.resources</LogicalName>
<Culture>fr-fr</Culture>
</EmbeddedResource>
</ItemGroup>

</Project>
");

// Iterate through each ItemGroup in the Project. There
are two.
foreach (BuildItemGroup ig in project.ItemGroups)
{
BuildItem itemToRemove = null;

// Iterate through each Item in the ItemGroup.
foreach (BuildItem item in ig)
{
// If the item's name is "RemoveThisItemPlease",
then
// store a reference to this item in a local
variable,
// so we can remove it later.
if (item.Name == "RemoveThisItemPlease")
{
itemToRemove = item;
}

// If the item's name is "EmbeddedResource" and it
has a metadata Culture
// set to "fr-fr", then ...
if ((item.Name == "EmbeddedResource") &&
(item.GetMetadata("Culture") == "fr-fr"))
{
// Change the item's Include path to
"FrenchStrings.fr.resx",
// and add a new metadata Visiable="false".
item.Include = @"FrenchStrings.fr.resx";
item.SetMetadata("Visible", "false");
}
}

// Remove the item named "RemoveThisItemPlease" from
the
// ItemGroup
if (itemToRemove != null)
{
ig.RemoveItem(itemToRemove);
}

// For each ItemGroup that we found, add to the end of
it
// a new item Content with Include="SplashScreen.bmp".
ig.AddNewItem("Content", "SplashScreen.bmp");
}

// The project now looks like this:
//
// <?xml version="1.0" encoding="utf-16"?>
// <Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
// <ItemGroup>
// <Compile Include="Program.cs" />
// <Compile Include="Class1.cs" />
// <Content Include="SplashScreen.bmp" />
// </ItemGroup>
// <ItemGroup>
// <EmbeddedResource
Include="FrenchStrings.fr.resx">
// <LogicalName>Strings.resources</LogicalName>
// <Culture>fr-fr</Culture>
// <Visible>false</Visible>
// </EmbeddedResource>
// <Content Include="SplashScreen.bmp" />
// </ItemGroup>
// </Project>
//
Console.WriteLine(project.Xml);
}
}
}

/Magnus
Anubhav Jain
3/16/2006 6:06:05 PM
Hi,

I am having few .net source files(.cs or .vb) and I want to dynamically =
generate the corresponding .net project file(.csproj or .vbproj) for =
them without using visual studio.So that I could be able to generate and =
compile the project on the enviroments where Visual Studio.Net is not =
installed.

Thanks and Regards,
Anubhav Jain=20
MTS
Persistent Systems Pvt. Ltd.
Ph:+91 712 2226900(Off) Extn: 2431
Mob : 094231 07471
www.persistentsys.com=20
Persistent Systems -Software Development Partner for Competitive =
Advantage. Persistent Systems provides custom software product =
development services. With over 15 years, 140 customers, and 700+ =
release cycles experience, we deliver unmatched value through high =
quality, faster time to market and lower total costs.

Anubhav Jain
3/16/2006 6:32:10 PM
Hi,
As I am generating the source files using CodeDOM so I dont have the
project file and which I want to generate for those source files.
Regards
Anubhav
[quoted text, click to view]

Michael Nemtsev
3/16/2006 6:42:32 PM
Hello Anubhav,

You need .NET SDK to compile files. This SDK includes csc and vbc that allow
u to compile your app and create solution files
AJ> Hi,
AJ>
AJ> I am having few .net source files(.cs or .vb) and I want to
AJ> dynamically generate the corresponding .net project file(.csproj or
AJ> .vbproj) for them without using visual studio.So that I could be
AJ> able to generate and compile the project on the enviroments where
AJ> Visual Studio.Net is not installed.
AJ>
AJ> Thanks and Regards,
AJ> Anubhav Jain
AJ> MTS
AJ> Persistent Systems Pvt. Ltd.
AJ> Ph:+91 712 2226900(Off) Extn: 2431
AJ> Mob : 094231 07471
AJ> www.persistentsys.com
AJ> Persistent Systems -Software Development Partner for Competitive
AJ> Advantage. Persistent Systems provides custom software product
AJ> development services. With over 15 years, 140 customers, and 700+
AJ> release cycles experience, we deliver unmatched value through high
AJ> quality, faster time to market and lower total costs.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

AddThis Social Bookmark Button