Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a project which contains 1 Class Library. I have a requirement, where I need to check some condition using C# code when user compiles/builds the code.

I want code to build successfully only when my phone is connected to my laptop. If the phone is not connected, the build should fail.

Thanks in advance.

What I have tried:

Based on my exploration ion Goggle, I did not find any relevant answer to this question.
Kindly help.
Posted
Updated 4-Sep-17 4:27am

Without using a prebuild event? Not possible using Visual Studio alone. The code being compiled will NOT be executed during the build.

You would have to custom write your own MSBuild file to add an executable to check your condition and terminate the build.

I don't have any examples, and, frankly, it's a very strange "requirement". If you think this can be used as some protection against someone else building your code, you're wrong. The MSBuild file is just a text file that can be edited like any other and your "requirement" can be easily removed.

Google for "Custom MSBuild Task" and start reading.
 
Share this answer
 
 
Share this answer
 
Comments
partha143 4-Sep-17 8:24am    
Sorry, I forgot to mention. I want to achieve this without Pre-Build event.
You can use pre-build events (Project - Properties - Build Events).

If you have some C# code that contains your checks, create a command line application using that code and return an appropriate value. Then execute it as pre-build event. The build will be stopped when the return value is not zero. These events are just command line executions like within batch files. You can check and set the errorlevel or use the exit command (where any other value than zero will again stop the build).
 
Share this answer
 

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