how to create solution file .sln ,.csproj ,assemblyinfo.cs dynamically - c#

I am creating code generation tool as windows forms application in visual studio 2010 in framework 4.0.
I want to build .csproj file, assemblyinfo.cs and .sln file dynamically including the some class files I have created so that when I click on this .sln file it is opened as project with all class files that I have generated.
It means that when I click on generatesolution button, a new project should be created with all folders automatically, that are created when we create new project from visual studio.
Please help me.

You would be better off using Visual Studio's automation capabilities rather than trying to generate the files yourself - there's an article on MSDN "Controlling the Solution and Its Projects" which gives an overview of the process of generating a olution, though it's a bit short on detail: there's also sample code in the documentation for the solution2 interface which shows how to create a console app solution, and an article on How to Programatically create projects.
Hopefully this is enough to get you started.

To create a project programatically, you can use Microsoft.Build.Evaluation.Project (MSDN documentation) and for the solution just use EnvDTE (MSDN documentation)

What is a solution ? I think a file which contains references and properties .
So what is the project file , It has some information about the forms and classes.
Basically we can write into a .sln file something like this :
Solution Name : Sol1
Projects : 1
References : System,System.Text,System.Linq
and for a project file :
Forms Count : 4
Classes : 8
Folders : 2
Resources : 0
Silver light : N
Web : N
Type : Windows Application
Directory : C:\Dir
Framework : 4
Parent Solution: Sln1
Or you can create data structures(class) for solution and project.
But it's better to Contact MSDN and get their own data structre and headers for .sln file.

Perhaps you could create a plain vanilla solution/project using Visual Studio with the bare minimum you need, then use the files created as a template for your code generation procedure.
You would need to change the project files to reference your generated files though. A .csproj file is just XML so it should be quite easier to alter that - look at how it adds sources files as a guideline. A .sln file would be a bit trickier to edit as that is a custom format, but I guess some kind of string replace/RegEx could work.

Related

How to run a C# project without the project file?

I have an C# source code of some software my company is using. This source code contains .xaml, .cs files, and one: App.config, packages.config. However there is no main project file that I can open in Visual Studio.
The original source code can not be found, and the programmer who made it has left the company long ago.
My question is - can I open such a project in C# somehow, without the main project file, so that I could make some changes and compile this application?
Just create a new WPF application project and add the existing files.
You can compile the code using the c# compiler csc (which you can find in the .NET Framework installation folder), or just create a new project in Visual studio and add the WPF files to it. The last option is the easiest one.

Add Visual Studio solution to existing solution in Visual Studio

I wrote an application in Visual Studio C# 2010, that I would like to import into another existing Visual Studio C# 2010 Application. How would I go about doing this?
For instance, I'd like to import the project into another, and basically copy/paste the interface from the application into a tabpage on a tab control I have.
Any assistance or advice on how to do this is greatly appreciated!
You should be able to copy the physical files using windows from the existing solution location to the new solution location.
Then once the copy is complete open Visual studio and tell it you want to add an existing project. Navigate to the folder where the files are on the filesystem and open the project file.
Once you save the solution it should from that point forward have the new projects in the new solution.
There are several ways to achieve this - Copy & Paste being one of them (but the least beautiful of course). A more promising one is the following:
Wrap the user interface you want to share in a User Control in the existing project. Also include the code behind logic in this user control. Include the user control in the project and make sure that everything works before continuing.
Add the existing project to the new solution. It is advised that you create a hierarchical structure in the file system for the solution so that all projects in the solution are located under a directory.
Reference the project from the project that wants to access the user interface. As the projects are in the same solution, you can add a project reference. This asserts that the projects are built together.
Place the user control on the tab page.
As an alternative, you can also pass on adding the project to the solution but only create a user control and add a binary reference from the other project.

How to add a Setup project through Visual Studio DTE?

I am trying to develop a template Visual Studio 2008 solution making use of the IWizard interface. So far I have been able to successfully add multiple projects to the solution. I am now looking to put in a Web Setup project into the solution which will already have the Project Output from a particular project specified.
I read that a .vdproj file cannot be part of a template, so looking at any other alternate options available.
One option that I tried but did not work was to save the contents of the .vdproj file within the wizard and write it out after the solution gets generated. I am wondering if I can use the Visual Studio ExecuteCommand to add a Project and add the Output Group in the setup project without displaying the UI.
Anyone tried this before?
I found the answer.
The method of saving the contents of the .vdproj file within the wizard works. However, before writing out the content to a file and adding it as a project, the GUID of the project which would be used in the Setup project (Project Output or Content Files) has to be replaced in the saved text.
Visual Studio adds a unique project GUID once the project gets added to the solution, and this GUID has to be read using the Visual Studio SDK and replaced in the actual .vdproj file text.

Shared AssemblyInfo for uniform versioning across the solution

I've read about this technique: Shared assembly info in VS projects - JJameson's blog
Basically it means to create a SharedAssemblyInfo.cs with versioning information about the assembly, and adding this file as Link to all projects of the solution, so the actual file resides only in 1 location on disk.
My question deals with 2 scenarios:
Existing solution that doesn't use this mechanism: Is there a way to easily add the ShareAssemblyInfo to all projects? (lets say i have a solution with 50 projects).
When creating a new project, by default a new AssemblyInfo.cs is created. However i'd like to link automatically to the SharedAssemblyInfo as well.
Is there any solution for this? what is the common practice?
It is possible to link to a shared assembly info file in VS 2010. Ashish Jain has a good blog post about it: Sharing assembly version across projects in a solution.
After creating the shared assembly info file at the solution level, his instructions for linking to it from a project are:
Right click on the project, in which you wish to add the Shared
assembly file, and select Add -> Existing Item...
Select the file “SharedAssemblyInfo.cs” from the solution folder.
Instead of Add, click on the the arrow next to Add and click “Add as
Link”
Drag down the added linked file alongside AssemblyInfo.cs in the
same folder.
Repeat steps 1 – 4 for all projects for which you wish to add shared
assembly file.
I've tried this and it works.
First point could be solved with simple text editor that could handle several files at once and find/replace. Just open all of your csproj in it and replace string <Compile Include="Properties\AssemblyInfo.cs" /> with
<Compile Include="..\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
Alternatively you could write a utility like that:
var files = Directory.GetFiles(yourSolutionDir, "*.csproj", SearchOption.AllDirectories);
foreach (var f in files) {
string contents = File.ReadAllText(f);
string result = contents.Replace("<Compile Include=\"Properties\\AssemblyInfo.cs\" />", putSecondStringHere_ItIsJustTooLong); // :)
File.WriteAllText(f, contents);
}
As for the second question... You could take a look at Visual Studio custom project templates , but I'm not sure it worth the efforts. You should IMO write test that will check this instead. It will be much simpler and outcome is actually almost the same.
UPD: About writing tests for checking solution/project files against some custom rules. Basically, sln/csproj format is simple enough to be parseable without much efforts. So if you want to have SharedAssemblyInfo.cs linked into every project - just parse csproj's and check that. Then put that checker in your build server and run it on each build. We have such system working currently and it costs something about two days to write but saved us many more (we have there more sophisticated rules and multi-solution project, so it was worth the efforts).
I won't write about this checking in detail here right now (it is not that short), but I'm going to write blog post about it soon - most probably till the end of this week. So, if you're interested - just check my blog soon :)
UPD: Here it is.
I have created an application to increment the file version automatically.
Download Applicaiton
add the following line to pre-build event command line
C:\temp\IncrementFileVersion.exe $(SolutionDir)\Properties\AssemblyInfo.cs
Build the project
To keep it simple the app only throws messages if there is an error, to confirm it worked fine you will need to check the file version in 'Assembly Information'
Note : You will have to reload the solution in Visual studio for 'Assembly Information' button to populate the fields, however your output file will have the updated version.
For suggestions and requests please email me at telson_alva#yahoo.com
This does not work for solution that has both C# and F# projects. c# project cannot reference shared f# file and vice versa.
The only option in this case is to make a separate project and refer to it from other projects

Multiple icons in executable in VS2010

I have Googled and found multiple ways of adding multiple icons into the executable, but they all seem to work for VS 2003-2005-2008, nothing for VS2010. I have not tried the Win32 resource with /win32res because I do not know how to use it (can't figure to get a good Google result for that either).
Any simple suggestion?
I've just created a simple tool to do exactly this without having to mess with .res files. It's a tiny utility which you can use as part of your Post-Build event and lets you add all icons files in a particular folder to your assembly. If we assume that you have a icons folder under your main project folder you can add the following post-build event:
C:\path\to\InsertIcons.exe $(TargetPath) $(ProjectDir)icons
A further description and a download can be found at http://einaregilsson.com/add-multiple-icons-to-a-dotnet-application/
This works for me:
http://www.codeproject.com/Tips/160885/How-to-Embed-Multiple-Icons-and-Color-Animated-Cur.aspx
If you're using visual studio 2012:
For C#.NET Here I found a good solution for this problem for c# projects as an example. But it only works in my C# projects
Create a new "Native Resource Template" from the File | New dialog box.
In project properties(project->application->resources) there is option to choose resource file (.res) rather than "Icon and manifest" which is selected by default (This option is visible only to C# projects!).
For VB.Net projects this link (Also mentioned here by Waldo) can be more helpful because in my visual 2012 there is no option to select/browse Native Resource Template(.res) files but you could manually change project definition file for vb.net project as described to compile project win a native win32 resource file:
Open your project file in notepad (*.vbProj) and add the following block:
<PropertyGroup>
<Win32Resource>assemblyWin32.res</Win32Resource>
</PropertyGroup>
The Code Project article explains how to create a "assemblyWin32.res" file.
https://www.codeproject.com/Tips/160885/%2fTips%2f160885%2fHow-to-Embed-Multiple-Icons-and-Color-Animated-Cur

Categories