Click here to Skip to main content
15,898,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an issue at hand.I have many projects lets say 100 cs projects and I want to write a C# program that would take all those projects and add them in a single visual studio solution file and also set the dependencies between all those projects and hence set the build order.I want to write a program to do that as I have many projects so I don't want to add them manually and set the dependencies among them based on references.There are no cyclic dependencies but among the 100 projects many are referenced in different different projects.So I want to somehow know the most independent project and somehow proceeding like that set the dependencies accordingly through
program.Please provide me solution for this.Please give sample code also in C#.

Thanks,
Mayank
Posted

First of all, it's not clear how you ended up having so many projects with dependencies, but without a solution file. Let's say you got bad legacy from previous developers, no matter.

First, you can find out how the solution file is structured: http://msdn.microsoft.com/en-us/library/bb165951%28v=vs.90%29.aspx[^].

It should be enough to put projects together in a solution file. Some simple experiments can help to find out the detail. The format is simple enough. The only problem is: to best of my knowledge, it is not standardized.

Now, projects and their dependencies. From your description it is not 100% clear if you have compiled projects or not. So, first, I'll quickly explain how to use compiled assemblies to find out their dependency. You should load each assembly in your program using System.Reflection.Assembly.LoadFrom and find out what assemblies are referenced using System.Reflection.Assembly.GetReferencedAssemblies. It will return an array of the instances of System.Reflection.AssemblyName objects, and, for each assembly name, compare it with assembly name of each of the other assemblies you got on output. If an assembly file is loaded, you can find assembly name from System.Reflection.Assembly.GetName(), if not — by using the static function System.Reflection.AssemblyName.GetAssemblyName to find an assembly name by a file name. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.aspx[^].

This way, you can get the dependencies between available assembly files. Note that in this approach we never used the location of the files on the file systems, we compare only the assembly names, which are not strings but the relatively detailed structures, especially if they are strongly named. Also note that for not strongly named assemblies, you can face the situation when you cannot find out correct dependency. Some projects are unrelated. If someone gave two different assemblies identical names (which is more or less likely if assemblies are not strongly named and extremely unlikely if they are), it won't guarantee you correct dependency.

But, from your description, it's more likely that your projects are not compiled. This is more difficult situation, but solvable, because assembly references are prescribed in the project files. If all your files comply with MSBuild project file standard, you can read those project files and find out references. It will give you dependencies. For this approach, you have comprehensive API from Microsoft, and the project file format is well standardized and formally documented.

Please start here:
http://msdn.microsoft.com/en-us/library/gg145008%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd393574%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/0k6kkbsd%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Mayank14 23-Oct-13 1:21am    
These 100 projects are now structured and managed but I still have a requirement to have single solution that will add and the projects and set the dependencies amongst them and hence the build order.Yes it is not compiled ,I am talking about a situation where suppose I am building this solution for the first time.Amongst these project say proj 1 might have reference for proj 2,3 like that then other projects again might have reference as proj 1 and some other projects ,so I wanted to write a piece of code that will do all these and will build the most independent proj first and then like that the others.
NETFramework\v4.0\Microsoft.Build.dll has project class which I can use I guess still ,it is not that much clear how I would achieve what I want but thanks .

-Mayank
Sergey Alexandrovich Kryukov 23-Oct-13 1:31am    
Okay, it was my guess that the project are not compiled. So, for now, you can skip the middle paragraph on reflection. For the whole work, I already depicted for you the whole general plan of all your work. If something is unclear, you can ask some follow-up questions, but you need to look at the referenced documentation, follow further link and do all the research on all tools you would need. The work is not very easy, but it should not be too big.

In future, you should try to avoid such awkward situations. (By the way, do you mean that not only you have no one common solution, but you have no solution files at all? How come? Because, if you have some solutions each integrated part of projects, it will make your problem really easy. Are you sure you don't have those files? If not, how people worked at those projects before you got them? If this is a real situation, someone made a huge mistake in the past...)

—SA
Mayank14 23-Oct-13 1:52am    
Ofcourse there is a solution file which had everything, the dependencies were set manually by looking at proj references for that project but now projects have been physically moved to new location better structured and also it is not that everytime I need to build them all,these projects are also in separate folders plus some new projects also got added in meantime so I could not use the old solution file.I have used project class before BuildItem and other things but I thought you guys might have some better and faster approach.

-Mayank
Sergey Alexandrovich Kryukov 23-Oct-13 1:59am    
Oh, then you simply need to merge solutions files mechanically, which is much easier. You only need to keep the locations of all the solution file relative to respective project files. You will need to combine the relative paths of projects relative to their solutions with relative paths of all solutions relative to your "master solution". If the set of solution files you already have is comprehensive, it will make all your work pretty simple. Couple of days of programming, I would say.
—SA
Right click at solution and select project dependanciees. Here you can set dependancies and built order
 
Share this answer
 
Comments
Mayank14 22-Oct-13 4:44am    
This thing I too know but, like I said there are many projects so I dont want to add them manually and set the order.I want to write a code in C#.
Lokesh Zende 13-Sep-21 11:37am    
Hey, Have you got the solution? I am also having the exactly similar case as yours.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900