Is there any way to get batch file name in c# console application which is running by that batch file. Suppose i have c# application eg. abc.exe and some batch file eg test.bat . So this test.bat file is running the abc.exe application , then i need to get the name of batch file i.e test .bat from c# code i.e from abc.exe
Asked
Active
Viewed 310 times
0
-
Can you clarify what you're asking? What do you mean by "running by that batch file"...do you mean if you can use C# to get the file name of all running .bat files? – Tyress Nov 28 '14 at 08:13
-
Ya sure i will explain you my question , suppose i have c# application eg. abc.exe and some batch file eg test.bat . So this test.bat file is running the abc.exe application , then i need to get the name of batch file i.e test .bat from c# code i.e from abc.exe – Indu Narayan Nov 28 '14 at 09:48
1 Answers
0
Supply the batch file name as a parameter to the C# program. You can access the parameter in the 'main' method.
More here:

Resource
- 524
- 4
- 16
-
ya i tried that way and also able to get but requirement is like that we are not able to enter any new parameters ,that are fixed , so we have to make changes in c# program only .So is there any way to get batch file name in c# code – Indu Narayan Nov 28 '14 at 09:40
-
I think you would have to look at currently executing applications or something. Not pretty and too open to error I think. One possibility is that you could change the name of your C# executable according to the nameof the calling batch file. It's easy to get the C# exe name in the C# program. Then match the exe name to a known list of possible batch file names. Also a nasty hack,but in the right conditions it could get you out of trouble. – Resource Nov 28 '14 at 10:24
-
@InduNarayan How about making a little C# launcher program that is launched by the .bat file and is given the .bat file's name plus the other parameters, and then it luanches the "real" program and does whatever is needed that is dependent on knowing the identity of the .bat file? – RenniePet Nov 28 '14 at 11:18
-
@RenniePet, I gather that it's the batch file that can't be changed, not the C# program. So the launcher would run in to the same issue. – Resource Nov 28 '14 at 11:25
-
@Indu Narayan, what is it that you can't change, the batch file or the C# Main function? You can access command-line parameters from anywhere in the program without changing Main, so if you can change the parameters the batch file sends you could consider that. – Resource Nov 28 '14 at 11:39
-
@user910683 yes you are correct only we can make changes to batch file .But as per requirement ,in a batch file input parameters are fixed ,we can not pass new input parameter like batchfile name . – Indu Narayan Nov 28 '14 at 13:28
-
You can get the batch file name from inside the batch file. See here: http://stackoverflow.com/questions/343518/finding-out-the-file-name-of-the-running-batch-file. Then pass the name to the C# program as a parameter. – Resource Nov 29 '14 at 13:26