First be happy you have written something useful enough to be pirated. .
This is little consolation, but remember, most human beings are thieves (given the opportunity).
Courts
Lotta guys saying "hire a lawyer".
Those guys prob never worked for a lawyer or seen a case through from inception to closure. Plus they are assuming that the legal system is sweet. If you are in a highly corrupt country, like in India, or China, good luck to you. It will take forever just to get to the court. Very hard to find a good lawyer. Most lawyers there are barely literate. I've had the pleasure of dealing with six. Three seemed very good. Of the other three - one was essentially convicted of embezzlement, and the other two were essentially imbeciles. so it's very, very hit and miss. Like most things: if you want a good lawyer, it will cost you.
If you are in the US then the person with the deepest pockets will likely be victorious - but it is time consuming and expensive, and you may (not) get your money back.
"a bad settlement is better than a good law suit". the adage still holds true. don't even attempt it unless you have money and time you are happy to lose.
Preventing privacy
If your app can be thrown on the cloud, then do so:
- Put your code behind a server
- Do not release the code.
- Expose "endpoints". Allow users to submit inputs. and give them the output.
Application needs to be client facing
Now you are at risk.
in this case, if possible give them an executable i.e. binary format. If you give them a .net dll, then even if you obfuscate that, you're still exposed because you're giving them IL code.
remove and put a few key functions on the cloud (i.e. follow the above step). Now piracy is not as easy / straight forward. Pirates will have to replicate the cloud functions, which is a pain. Perhaps make post requests to an online data store. many ways you can skin the cat but the principle is the same: make sure that there is some source code hidden in the cloud that cannot be easily replicated.
Don't forget to charge for your services. If it's useful, people will be incentivised to pay for it.