-2

My c# program is a text editor which needs file name as an input parameter. In other words, I would like to start the c# EXE from a BAT file specifying which file to open. For example: "call C:\Temp\MyDotNetApp File1", where 'File1' is the input parameter for the program in C#.

Is this possible in C#? I can't find any tutorial on internet.

My code:

namespace CSVEditor{
     public partial class Form1 : Form { 
     public static string TAG = "";
     public static string FileLinnk = "";
}

public Form1()
    {
        InitializeComponent();          
    }
private void Form1_Load(object sender, EventArgs e)
    {
        //Input file to read
        File = "File1";// <----- This needs to be the input parameeter from BAT file.

        //
        FileLink = @"c:\temp" + File + ".csv";

        ReadCSV(FileLink);
    }

Cheers.

3
  • msdn.microsoft.com/en-us/library/… Commented Jan 26, 2018 at 10:07
  • Can't you just pass the string in to the constructor? Commented Jan 26, 2018 at 10:07
  • the problem is not passing the string to the form, but you need to read the parameters .. read the link I posted Commented Jan 26, 2018 at 10:08

1 Answer 1

1

Just use Environment.GetCommandLineArgs;

string[] args = Environment.GetCommandLineArgs();
Sign up to request clarification or add additional context in comments.

3 Comments

As stated in the duplicate: stackoverflow.com/a/18448925/2598770
You don't want to do this in the form itself, because then you can't use it to edit arbitrary files (e.g. File -> Open...).
Thanks, this works for me: stackoverflow.com/a/37457463/5918997

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.