We have a 3rd party login dialog which will skip the login prompt if the login data is passed in via command line arguments. This is used when an application is launched from within the main 3rd party software.
The custom app I am writing should provide users with a button to change their login info, however since the app is launched with the login info provided in the command line args, the login dialog never appears when the button is clicked.
Is it possible to clear or reset Environment.GetCommandLineArgs() from the code?
Edit
I ended up simply restarting the application prior to startup if login info existed in the command line. This makes the 3rd party login dialog actually show up instead of automatically using the login info provided in the command line arguments.
I’m accepting Jim’s answer because I feel it is the most complete answer to my question, although Oded’s answer is also a viable alternative.
What you’re asking can’t be done in .NET because the
Environmentclass caches the command line and there’s no property accessor for setting it. (More correctly, the startup code caches the command line andEnvironment.CommandLinecalls into the runtime to get that cached value.)In a native Windows application, the
GetCommandLine()API function returns a pointer to the command line that the operating system presented to the program. A program can callCommandLineToArgvWto parse the command line into the standardargvandargcparameters familiar to C and C++ programmers.The
Environmentclass uses something similar. When you callEnvironment.GetCommandLineArgs, it accesses theEnvironment.CommandLineproperty and then calls the windows functionCommandLineToArgvWto parse the command line. ButEnvironment.CommandLinedoesn’t get its value fromGetCommandLine(). Instead, the program gets the Windows command line at startup (by callingGetCommandLine()), and then saves it.This is unfortunate, because you can modify the value that
GetCommandLinereturns, as demonstrated by this little snippet:If you run that, you’ll find that
Environment.CommandLinereturns the same string both times, whereas the second time you callGetCommandLine, you’ll get back the stringham and swiss on rye.Even if the above did work, there’s no guarantee that it would solve your problem. The 3rd party control might parse the command line, cache the login information, and never parse the command line again.