C# - Arguments for application

后端 未结 5 417
野趣味
野趣味 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:46

    You are looking for commandline arguments, aren't You?

    Here You will find some examples: http://www.csharphelp.com/archives/archive273.html Here more: http://www.google.com/search?hl=en&q=%22c%23%22+command+line+arguments&aq=f&oq=&aqi=g10

    0 讨论(0)
  • 2021-01-27 08:54

    here is a snippet

    class myclass
    {
      public static void main(string [] args)
      {
        if(args.Length == 1)
        {
           if(args[0] == "/i")
           {
             Console.WriteLine("Parameter i");
           }
        }
      }
    }
    

    The %1 is actually syntax for BAT files to pass the parameter through. So if you see program.exe %1 in a file named cmd.bat, you can call cmd.bat /i and the /i will be passed through to program.exe

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-27 08:55

    These are called command-line arguments. There's a good tutorial on MSDN on how to use them.

    This example should get you started:

    class TestClass
    {
        static void Main(string[] args)
        {
            // Display the number of command line arguments:
            System.Console.WriteLine(args.Length);
        }
    }
    
    0 讨论(0)
  • 2021-01-27 09:00

    Command line arguments.

    On c# you can find them on

    static void Main(string[] args)
    

    Or from anywhere using

    Environment.GetCommandLineArgs()
    
    0 讨论(0)
提交回复
热议问题