By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.



Mar 16 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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

Mar 16 '06 #2

P: n/a
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
"SunYour" <su*****@hotmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
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

Mar 16 '06 #3

P: n/a
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

Mar 16 '06 #4

P: n/a
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

Mar 16 '06 #5

P: n/a
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
Mar 16 '06 #6

P: n/a
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/lib...a5(VS.80).aspx

Pay particular attention to:
http://msdn2.microsoft.com/en-us/lib...68(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.t argets"
/>
output.WriteStartElement("Import", ns)
output.WriteAttributeString("Project",
"$(MSBuildBinPath)\Microsoft.VisualBasic.targe ts")
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
"Anubhav Jain" <an**********@persistent.co.in> wrote in message
news:OK**************@TK2MSFTNGP11.phx.gbl...
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.


Mar 17 '06 #7

P: n/a
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

Mar 20 '06 #8

P: n/a
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.t argets" />

While cs has this import statement:

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

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
"SunYour" <su*****@hotmail.com> wrote in message
news:11**********************@t31g2000cwb.googlegr oups.com...
| 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
|
Mar 20 '06 #9

P: n/a
u can use visual studio automation to generate the project in .net 2 . Check
whether it is what u are looking for. But i guess u need VisualStudio.NET
2005 installed to make this code work. The code and references are for
VS2005 projects. U can search for VS2003 projects in MSDN.

References:(MSDN)
How to: Get References to the DTE and DTE2 Objects
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.
en/dv_extcore/html/c92e3c8e-82e6-4a67-85da-e43c50ffd8e4.htm

How to: Add References to the EnvDTE and EnvDTE80 Namespaces
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.
en/dv_extcore/html/cf76e015-8e3d-4148-8d4e-4f7f48e29f5c.htm

VSProject2.References Property
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.
en/dv_vslangproj80/html/P_VSLangProj80_VSProject2_References.htm

//Code u can use to generate VB.NET or C# projects

//projType == "ClassLibrary.zip" for classbibrary projects
//The project templates for all the languages can be found in
<drive>\Program Files\Microsoft Visual Studio
8\Common7\IDE\ProjectTemplates\CSharp for CSharp templates

<drive>\Program Files\Microsoft Visual Studio
8\Common7\IDE\ProjectTemplates\VisualBasic for VisualBasic templates

for references:

//http://msdn2.microsoft.com/en-us/library/ms228767.aspx
//http://msdn2.microsoft.com/en-us/library/7h8k229a.aspx

using VSLangProj;
using EnvDTE80;
using VSLangProj80;
using EnvDTE;

void BuildALibProject(bool bCSharp, string projType)
{

string projDir = @"C:\Temp";
string projName = "TestProj";

// Create a new instance of Visual Studio.
EnvDTE80.DTE2 myDTE2;
myDTE2 = (EnvDTE80.DTE2)Microsoft.VisualBasic.Interaction.
CreateObject("VisualStudio.DTE.8.0", "");

Solution2 soln = (Solution2)myDTE2.Solution;
// soln.Create(projDir , "TestProj.sln");
string lang = string.Empty;
string defmember = string.Empty;
string ext = string.Empty;
if (bCSharp == true)
{
lang = "CSharp";
defmember = "Class1.cs";
ext = "*.cs";
}
else
{
lang = "VisualBasic";
defmember = "Class1.vb";
ext = "*.vb";
}
string projectdir = projDir + @"\Test";

string PrjTemplatePath = soln.GetProjectTemplate(projType ,lang)
;
soln.AddFromTemplate(PrjTemplatePath, projectdir,projName, false)
;

Project prj;
ProjectItems prjItems;

prj = soln.Projects.Item(1); //to get the Project type
string filesdir = "C:\CsharpFiles; //Directory containing cs or
vb files to add to the project
prjItems = prj.ProjectItems;
string[] csfiles = Directory.GetFiles(filesdir , ext);
foreach (string csfile in csfiles)
{
prjItems.AddFromFile(csfile);
}
//other methods u can check for
// prjItems.AddFromFileCopy
// prjItems.AddFromDirectory
ProjectItem dummyclass = prjItems.Item(defmember);
dummyclass.Remove();
VSProject2 myVSProject = (VSProject2)prj.Object;
References refmanager = myVSProject.References;
//to add the references
refmanager.Add("Nunit.framework");
myVSProject.Refresh();

prj.Save(prj.FileName);
File.Delete(projDir + @"\" + dummyclass.Name);
}

Murali
Aug 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.