Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we get argv[0] in C#?

Tags:

c#

linux

argv

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].

like image 457
jw_ Avatar asked May 01 '19 03:05

jw_


People also ask

What is argv 0 in C?

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.

Why is argv 0 in command line arguments?

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.

Is argv 0 the program name?

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.

What is the use of ARGV in C++?

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?

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.

What is the value of argc in C?

*/ } 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)

What is the difference between argc and argv in Linux?

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.


1 Answers

Environment.CommandLine and Environment.GetCommandLineArgs() should contain the full and unmodified command line.

like image 70
kalimag Avatar answered Sep 30 '22 09:09

kalimag