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

A

Anubhav Jain

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
MTS
Persistent Systems Pvt. Ltd.
Ph:+91 712 2226900(Off) Extn: 2431
Mob : 094231 07471
www.persistentsys.com
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.
 
S

SunYour

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
 
A

Anubhav Jain

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
 
K

Kalpesh

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
 
S

SunYour

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
 
M

Michael Nemtsev

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
 
J

Jay B. Harlow [MVP - Outlook]

Anubhav,
In addition to the other commetns:

A .net project file(.csproj or .vbproj) is simply an XML file, that adheres
to a specific schema, you could use XmlTextWriter (or XmlDocument if you
like, or even StreamWriter) to create the file. I have not looked into the
Microsoft.Build namespace far enough to see if creating entire project files
is possible...

For information on how MS Build works see:

MS Build
http://msdn2.microsoft.com/en-us/library/wea2sca5(VS.80).aspx

Pay particular attention to:
http://msdn2.microsoft.com/en-us/library/ms171468(VS.80).aspx


For reference information see:

MS Build reference:
http://msdn2.microsoft.com/en-us/library/0k6kkbsd.aspx


Generally what I would do is look at a couple of existing .csproj & .vbproj
files to get a feel of the layout, then write my .csproj or .vbproj
generator to match that.

Here is the start of a .vbproj generator:

Private Sub CreateVBProject()
Const ns As String =
"http://schemas.microsoft.com/developer/msbuild/2003"
Dim output As XmlWriter = XmlWriter.Create("Sample.vbproj")
'<?xml version="1.0" encoding="utf-8"?>
output.WriteStartDocument()
'<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
output.WriteStartElement("Project", ns)
' <PropertyGroup>
output.WriteStartElement("PropertyGroup", ns)
' <Configuration Condition=" '$(Configuration)' == ''
">Debug</Configuration>
output.WriteStartElement("Configuration", ns)
output.WriteAttributeString("Condition", " '$(Configuration)' == ''
")
output.WriteString("Debug")
output.WriteEndElement() ' Configuration
' <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
' <ProductVersion>8.0.50727</ProductVersion>
' <SchemaVersion>2.0</SchemaVersion>
' <ProjectGuid>... this appears to be unique to a project
....</ProjectGuid>
' <OutputType>Exe</OutputType>
' <StartupObject>Sample.MainModule</StartupObject>
' <RootNamespace>Sample</RootNamespace>
' <AssemblyName>Sample</AssemblyName>
' <MyType>Console</MyType>
' </PropertyGroup>
output.WriteEndElement() ' PropertyGroup

output.WriteStartElement("PropertyGroup", ns)
' <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Debug|AnyCPU' ">
' <DebugSymbols>true</DebugSymbols>
' <DebugType>full</DebugType>
' <DefineDebug>true</DefineDebug>
' <DefineTrace>true</DefineTrace>
' <OutputPath>bin\Debug\</OutputPath>
output.WriteElementString("OutputPath", ns, "bin\Debug\")
' <DocumentationFile>Sample.xml</DocumentationFile>
'
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
' </PropertyGroup>
' <PropertyGroup Condition=" '$(Configuration)|$(Platform)' ==
'Release|AnyCPU' ">
' <DebugType>pdbonly</DebugType>
' <DefineDebug>false</DefineDebug>
' <DefineTrace>true</DefineTrace>
' <Optimize>true</Optimize>
' <OutputPath>bin\Release\</OutputPath>
' <DocumentationFile>Sample.xml</DocumentationFile>
'
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
' </PropertyGroup>
output.WriteEndElement() ' PropertyGroup

' <ItemGroup>
' <Reference Include="System" />
' <Reference Include="System.Data" />
' <Reference Include="System.Deployment" />
' <Reference Include="System.Xml" />
' </ItemGroup>
' <ItemGroup>
' <Import Include="Microsoft.VisualBasic" />
' <Import Include="System" />
' <Import Include="System.Collections" />
' <Import Include="System.Collections.Generic" />
' <Import Include="System.Data" />
' <Import Include="System.Diagnostics" />
' </ItemGroup>

' <ItemGroup>
output.WriteStartElement("ItemGroup", ns)
' <Compile Include="MainModule.vb" />
For Each file As String In IO.Directory.GetFiles("Processors",
"*.vb")
output.WriteStartElement("Compile", ns)
output.WriteAttributeString("Include", file)
output.WriteEndElement() ' Compile
Next
' <Compile Include="My Project\AssemblyInfo.vb" />
' <Compile Include="My Project\Application.Designer.vb">
' <AutoGen>True</AutoGen>
' <DependentUpon>Application.myapp</DependentUpon>
' </Compile>
' <Compile Include="My Project\Resources.Designer.vb">
' <AutoGen>True</AutoGen>
' <DesignTime>True</DesignTime>
' <DependentUpon>Resources.resx</DependentUpon>
' </Compile>
' <Compile Include="My Project\Settings.Designer.vb">
' <AutoGen>True</AutoGen>
' <DependentUpon>Settings.settings</DependentUpon>
' <DesignTimeSharedInput>True</DesignTimeSharedInput>
' </Compile>
' </ItemGroup>
output.WriteEndElement() ' ItemGroup

' <Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets"
/>
output.WriteStartElement("Import", ns)
output.WriteAttributeString("Project",
"$(MSBuildBinPath)\Microsoft.VisualBasic.targets")
output.WriteEndElement() ' Import

' <!-- To modify your build process, add your task inside one of
the targets below and uncomment it.
' Other similar extension points exist, see
Microsoft.Common.targets.
' <Target Name="BeforeBuild">
' </Target>
' <Target Name="AfterBuild">
' </Target>
' -->
'</Project>
output.WriteEndElement() ' Project
output.WriteEndDocument()
output.Close()
End Sub


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


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
MTS
Persistent Systems Pvt. Ltd.
Ph:+91 712 2226900(Off) Extn: 2431
Mob : 094231 07471
www.persistentsys.com
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.
 
S

SunYour

The "default" path for the project file definition:

C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\1033\MSBuild

Microsoft.Build.Core.xsd and
Microsoft.Build.Commontypes.xsd


It looks like the project files for cs and vb are the same in the 2005
edition...

/Magnus
 
J

Jay B. Harlow [MVP - Outlook]

SunYour,
Yes they both follow the same schema. The articles I mentioned point out the
similarities & differences.

Looking at it, I think I would favor Microsoft.Build namespace as you
originally mention over "rolling my own" writer as I mentioned. However I
find knowing both methods beneficial...


The major difference is that cs imports a cs specific targets file, while vb
imports a vb specific targets file. Both targets files import a common
targets file...

For example, vb has this import statement:

<Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />

While cs has this import statement:

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

If you look at the above files, they both import:

<Import Project="Microsoft.Common.targets" />

Plus defines the task to compile VB files (verses the task to compile CS
files in the CSharp targets file).

The above targets files are found in the same folder as the framework,
normally C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| The "default" path for the project file definition:
|
| C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas\1033\MSBuild
|
| Microsoft.Build.Core.xsd and
| Microsoft.Build.Commontypes.xsd
|
|
| It looks like the project files for cs and vb are the same in the 2005
| edition...
|
| /Magnus
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top