2

What does this code do in this example, what does args[0] store? If I want to access the path for my directory, but I can not though i'm pretty sure im making a mistake on the same line.

string directoryPath = args[0];

This is my code:

class Program
    {
        static void Main(string[] args)
        {

                string directoryPath = args[0];
                string[] filesList, filesListTmp;
                IFileOperation[] opList = { new FileProcNameAfter10(),
                                            new FileProcEnc(),
                                            new FileProcByExt("jpeg"),
                                            new FileProcByExt("jpg"),
                                            new FileProcByExt("doc"),
                                            new FileProcByExt("pdf"),
                                            new FileProcByExt("djvu")
                                            };
                if (Directory.Exists(directoryPath))
                {
                    filesList = Directory.GetFiles(directoryPath);
                    while (true)
                    {
                        Thread.Sleep(500);
                        filesListTmp = Directory.GetFiles(directoryPath);
                        foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
                        {
                            Console.WriteLine(elem);
                            foreach (var op in opList)
                            {
                                if (op.Accept(elem)) op.Process(elem);
                            }
                        }
                        filesList = filesListTmp;
                        if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
                    }
                }
                else
                {
                    Console.WriteLine("There is no such directory.");
                    Console.ReadKey();

                }


        }
    }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
thanks
  • 61
  • 2
  • 4
  • 12

6 Answers6

3

what does args 0 store in this program

Its command line parameter, you get value in it if you execute your exe from command line with parameters.

For debugging purpose you can send it through visual studio as well, go to project properties, Debug and under start option specify.

enter image description here

Habib
  • 219,104
  • 29
  • 407
  • 436
3

OK, so args[0] represents the first command-line parameter passed to the program - so in short we have no idea what it represents in your case. But consider this command-line:

MyProgram.exe

that would pass nothing into the args[] and therefore there wouldn't be any index of 0. Now consider this command-line:

MyProgram.exe Hello

that would pass Hello into the args[] and therefore the value at index 0 would be Hello. Now let's consider this command-line:

MyProgram.exe Hello World

that would pass Hello and World into the args[] and therefore the value at index 0 would be Hello and the value at index 1 would be World. Now, another thing to remember is to " parameters, especially when necessarily dealing with paths:

MyProgram.exe "C:\MyPath\ToSomewhere"
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
2

If I want to access the path for my Directory [...]

If you want to get the Location of your application, you can try

System.Reflection.Assembly.GetExecutingAssembly().Location

See this question as well: How can I get the application's path in a .NET console application?

Community
  • 1
  • 1
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
1

you need to set the command line parameter to something so that the arg[0] is populated with something. You can do this from the Debug tab of your project properties and setting the Command Line Arguments field. This will allow you to debug your application as if a command line was specified from the prompt when run outside of the IDE. Generally you should always check for the existence of a parameter before you try using it like;

if (args != null && args.Length > 1)
{
    // now you know there's thing in "args[0]"
}
DiskJunky
  • 4,750
  • 3
  • 37
  • 66
1

args[0] contains the first commandline parameter passed when starting your application.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
1

args[0] is the first argument passed during execution i.e.

C:\Path to your program\Program.exe "Your directory path"

Ravinder Gujiri
  • 1,494
  • 10
  • 14