Link to home
Start Free TrialLog in
Avatar of DomyZ
DomyZ

asked on

How to build/compile a project programmatically. Visual Studio 2019, C#

Hello,

I have a solution with 2 projects. I want to build/compile one project from another. To be more exact I want a user that will be using ProjectNR1 to enter the information then the information should be passed to ProjectNR2 and then the project(ProjectNR2) builds/compiles. So the question I have is: How to compile a VisualStudio 2019 project programmatically using C# (It's a windows forms application). I have been trying to figure out this all day with no luck.
Avatar of Robb Hill
Robb Hill
Flag of United States of America image

Avatar of DomyZ
DomyZ

ASKER

I have seen it, but it doesn't fit my situation. I want to compile/build the entire project, not just the source code. I have timers and other stuff like that I think won't work if I just compile the source code. (Its a windows forms applications)
Never done it myself, and it sounds like an "advanced" procedure, but you might take a look at MSBUILD.



»bp
>>I want to compile/build the entire project, not just the source code.

Erm.  You do understand that a compile/build converts the source code into code the computer understands.  When you compile a project you ARE compiling the source code.
You can use a ProjectInstance to compile a project.
new Microsoft.Build.Execution.ProjectInstance(PROJECT_FILE_PATH).Build();

Open in new window

Avatar of DomyZ

ASKER

I tried using
new Microsoft.Build.Execution.ProjectInstance(PROJECT_FILE_PATH).Build();

Open in new window

but I keep getting an error "The project file could not be loaded. Data at the root level is invalid. Line 2, position 1.".
Where the "PROJECT_FILE_PATH" is I put a path to my "Project.sln" file. Am I doing this wrong or there is another reason why I'm getting this error?
Avatar of DomyZ

ASKER

>> Erm.  You do understand that a compile/build converts the source code into code the computer understands.  When you compile a project you ARE compiling the source code.

I understand it, but in my case, I'm using windows forms application with timers, datasets table adapters, etc. And I'm pretty sure that if I only compile the Form1.cs source code these things wouldn't work anymore. Am I wrong?
Correct, you are wrong.  All source code including that for controls such as timers, other classes such as adaptors are compiled (when necessary - certain things are already compiled as part of the .net runtime environment) with the form.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Where the "PROJECT_FILE_PATH" is I put a path to my "Project.sln" file.

You have to put the project file path, not the solution.
If you use ProjectInstance, you compile each project one after the other.