With C/C++, we can get argv[0]:
printf("%s\n",argv[0])
In C#, args begins with argv[1].
Non of bellow API gives exactly argv[0], at least under Linux:
AppDomain.CurrentDomain.FriendlyName: only gives the name, no path
Process.GetCurrentProcess().ProcessName: gives wrong result with symbol link, and no path
Process.GetCurrentProcess().MainModule.FileName: gives wrong result with symbol link, and the path is always absolute
FYI: under Linux with the above C/C++ code (whose result is treated as the golden standard here), it prints the exact path (absolute or relative) that is used to invoke the program, and if you invoke the program through any symbol link, the symbol link's name is printed instead of the real program.
I ask this question since I try to write a wrapper program using C# under Ubuntu, which should pass argv[0] through to be fully transparent in case the wrapped program's behavior depends on argv[0].
By convention, argv[0] is the command with which the program is invoked. argv[1] is the first command-line argument. The last argument from the command line is argv[argc - 1] , and argv[argc] is always NULL.
argv stands for argument vector. It is an array of pointers to the strings which represent the command line arguments. You do not have to call them argc or argv, but this is the custom. *argv[0] represents a pointer to the name of the program being run.
By convention, argv[0] is the filename of the program. However, on Windows it's possible to spawn a process by using CreateProcess . If you use both the first and second arguments ( lpApplicationName and lpCommandLine ), argv[0] may not be the executable name.
argv (ARGument Vector) is array of character pointers listing all the arguments. If argc is greater than zero,the array elements from argv to argv [argc-1] will contain pointers to strings. Argv is the name of the program, After that till argv [argc-1] every element is command -line arguments.
How can I get argv [] as int? Bookmark this question. Show activity on this post. Show activity on this post. argv [1] is a pointer to a string. To get an integer from a string you have first to convert it. Use strtol to convert a string to an int.
*/ } argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, value of argc would be 2 (one for argument and one for program name)
int main (int argc, char *argv []) Here, argc parameter is the count of total command line arguments passed to executable on execution (including name of executable as first argument). argv parameter is the array of character string of each command line argument passed to executable on execution.
Environment.CommandLine
and Environment.GetCommandLineArgs()
should contain the full and unmodified command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With