views:

71

answers:

2

Hey,

I can't execute my application through cmd, when the application is trying to read the argument which was sent to it (text file), it fails...

When I'm trying to execute it through the IDE (vs2008), it works ok...

That's what I did in the main method:

static void Main(string[] args)
{
    int choice = 0;

    if (args.Length == 0)
       choice = 1;
    else
       choice = 2;

    switch(choice)
    {
       case 1:
          string[] text = Directory.GetFiles("allText");
          Console.WriteLine(DateTime.Now.ToString());

          foreach (string fileName in text)
          {
             string substring = fileName.Substring(8);

             ReadData_Logic rd_l = new ReadData_Logic(substring);
             rd_l.runThreadsAndDecrypt();
             rd_l.printKey(substring.Substring(0, fileName.Length - 15).Insert(0, "encryptedKey\\") + "_result.txt");
           }

           Console.WriteLine(DateTime.Now.ToString());
           break;

       case 2:
          Console.WriteLine(DateTime.Now.ToString());
          string fileName = args[0];
          Console.WriteLine(fileName); **<--- for debug, here i do see the correct file name**
          ReadData_Logic rd_l = new ReadData_Logic(fileName);
          rd_l.runThreadsAndDecrypt();
          rd_l.printKey(fileName + "_result.txt");

          Console.WriteLine(DateTime.Now.ToString());
          break;
    }
}

What's wrong with the code ? thanks

+1  A: 

Are you passing the full path to the file as: "C:\My Documents\MyFile.txt" ?

You can only pass the file name as MyFile.txt if the file is in the same directory where you are executing the cmd/running your app.

Leniel Macaferi
and watch out for those paths with spaces in them, like `C:\My Documents......` !!
marc_s
Yes, marc_s is correct... take care with paths that have white spaces... put " " to pass the path to the file as in "C:\My Documents\MyFile.txt".
Leniel Macaferi
A: 

What happens / what gets output when you add these few lines:

static void Main(string[] args)
{
    Console.WriteLine("Count of arguments: {0}", args.Length);

    int choice = 0;

    if (args.Length == 0)
       choice = 1;
    else
       choice = 2;

    Console.WriteLine("Choice now is: {0}", choice);

What do you get???

marc_s
fixed the problem ,thanks anyway