How to organize the launch of the application from the command line? Suppose we have a ZipApp application that uses the parameters run, name1 and name2. I want to be able to run the program from the command line in two versions:

"C: / Program / ZipApp zip file1 archive1" or "C: / Program / ZipApp unzipping arhive1 file1"

Here is a sketch of the code that does not work:

void Main(string[] args) { if (args[0] != null && args[1] !=null & args[2] != null) { string run = args[0]; string name1 = args[1]; string name2 = args[2]; switch (run) { case "zipping": string ZipFileName = name1; string ZipArchName = name2; Console.WriteLine("Создаём архив из файла {0} в архив {1}", ZipFileName,ZipArchName); Zipping(ZipFileName, ZipArchName); break; case "unzipping": string UnZipArchName = name1; string UnZipFileName = name2; Console.WriteLine("Разархивируем архив {0} в файл {1}",UnZipArchName,UnZipFileName); Unzipping(UnZipArchName, UnZipFileName); break; } } else { Console.WriteLine("Параметры запуска не указаны или указаны не верно"); } Console.ReadLine(); } 

the trouble is that when you try to start an application from VS, the compiler swears:

 Program does not contain a static 'Main' method suitable for entry point 

Tell me what am I doing wrong?

  • 3
    Add static before void Main (string [] args). msdn.microsoft.com/en-us/library/acy3edy3.aspx - Murad
  • That is, after this, the program should theoretically be taken from the command line? but you can’t check it out from VS & because when you start it in VS it’s just a black window - Garrus_En
  • four
    @ Anton Lihatsky, they said about static. about launching from the studio, in the properties of the project on the "Launch" tab, add command line parameters - Umed

1 answer 1

As @Umed correctly noted, to specify the launch parameters from under VS, in the properties of the project this can be done here.

example of specifying program launch arguments

The program should be divided into methods, according to the principle: one task - one method. It is easier to write and test, find and correct errors.

  static void Main(string[] args) { //работаем, если есть необходимое кол-во аргументов if (args.Length == 3) { SelectAndRunJob(args[0], args[1], args[2]); } else { Console.WriteLine("Параметры запуска указаны не верно"); } Console.ReadKey(); } private static void SelectAndRunJob(string typeJob, string fileInput, string fileOutput) { switch (typeJob) { case "zipping": MakeZip(fileInput, fileOutput); break; case "unzipping": MakeUnzip(fileInput, fileOutput); break; default: Console.WriteLine("Первый аргумент указан вами не верно."); Console.WriteLine("Следует указать zipping или unzipping"); break; } } private static void MakeZip(string fileInput, string fileOutput) { //прежде чем что-либо делать //проверим существование входного файла if (!File.Exists(fileInput)) { Console.WriteLine("Архивируемый файл указан не верно."); return; } //далее остальное... } private static void MakeUnzip(string fileInput, string fileOutput) { //прежде чем что-либо делать //проверим существование входного файла if (!File.Exists(fileInput)) { Console.WriteLine("Архивный файл указан не верно."); return; } //далее остальное... } 

Now you can separately run and test the job of defining arguments and archiving / unarchiving.

  • And how to check in the MakeUnzip method, is the fileInput an archive? - Garrus_En
  • @ AntonLihatsky and what is your archiver? Zip or other? - Bulson
  • one
    @ Anton Lihatsky on the other hand, but why check? It is necessary to unzip an attempt to wrap it in try/catch and in case of an error, to inform the user that the file format is incorrect or the file is damaged. - Bulson
  • did like this: try {this and that} catch (InvalidDataException) {Console.WriteLine ("maybe the file is not an archive")} So valid? ) - Garrus_En
  • one
    @AntonLihatsky well, yes, only the phrase: "maybe the file is not an archive at all" is good if you write a program for yourself or friends. - Bulson