Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify the Environment.CommandLine property?

Tags:

c#

.net

vb.net

For unit tests I would like to mimic different commandline arguments. How do I modify the commandLine args to my program at runtime (looked around but can't find the trick)? Basically I am trying to find a way to modify the contents of the READONLY property Environment.CommandLine.

like image 839
Denis Avatar asked May 23 '26 00:05

Denis


2 Answers

You can't, this is 'hard' read-only. It comes out of Windows, GetCommandLine API function, it doesn't allow modifying it either. You'll have to mock it.

like image 94
Hans Passant Avatar answered May 24 '26 13:05

Hans Passant


It sounds to me as if you're trying to test things the hard way. Presumably somewhere you have a function that is reading directly from Environment.CommandLine. Refactor that method to take a string parameter (which in the real application will be populated from Environment.CommandLine), and test that method independently.

like image 40
Richard J Foster Avatar answered May 24 '26 14:05

Richard J Foster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!