C# - Arguments for application

后端 未结 5 463
野趣味
野趣味 2021-01-27 08:02

How can I make it so when there are arguments added to the end of the program name it does a specific method or whatever?

Also, is there a name for this?

Example

5条回答
  •  日久生厌
    2021-01-27 08:54

    There are a few things you mention here.

    First of all, you want command-line arguments. How you get them depends on the type of application. For example in a console application you define the main method like this:

    public static void Main(string[] args) {
        ...
    }
    

    where you can access all command-line arguments that were given to the program in the args array.

    In other project types you may need to resort to Environment.GetCommandLineArgs.

    Furthermore, you talk about %1 which has, at first, nothing to do with your specific problem here. It's used in batch files and in the registry when setting file type associations. It stands for the first command-line argument in batches, or the document you want to open for file type associations.

    So when setting a file type association for your program you may use the following commands (on the Windows command line):

    assoc .myExt=MyProgram
    ftype MyProgram=myprogram.exe /i %1
    

提交回复
热议问题