args 0在这个程序中存储了什么

时间:2013-03-04 12:44:17

标签: c#

此代码在此示例中执行了什么操作,args[0]存储了什么?如果我想访问我的目录的路径,但我不能,但我很确定我在同一行上犯了错误。

string directoryPath = args[0];

这是我的代码:

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();

                }


        }
    }

6 个答案:

答案 0 :(得分:3)

  

args 0在此程序中存储了什么

它的command line parameter,如果从带有参数的命令行执行exe,就可以获得它的价值。

出于调试目的,您也可以通过visual studio发送它,转到项目属性,Debug和启动选项指定。

enter image description here

答案 1 :(得分:2)

好的,所以args[0]表示传递给程序的第一个命令行参数 - 所以简而言之,我们不知道它在你的情况下代表什么。但请考虑以下命令行:

MyProgram.exe

不会将任何内容传递到args[],因此不会有0的任何索引。现在考虑这个命令行:

MyProgram.exe Hello

会将Hello传递到args[],因此索引0的值将为Hello。现在让我们考虑一下这个命令行:

MyProgram.exe Hello World

会将HelloWorld传递到args[],因此索引0的值为Hello,索引为{{1}的值将是1。现在,要记住的另一件事是World参数,特别是当必须处理路径时:

"

答案 2 :(得分:2)

  

如果我想访问我的目录路径[...]

如果您想获取应用程序的位置,可以尝试

System.Reflection.Assembly.GetExecutingAssembly().Location

也可以看到这个问题:How can I get the application's path in a .NET console application?

答案 3 :(得分:1)

您需要将命令行参数设置为某个值,以便arg[0]填充某些内容。您可以从项目属性的“调试”选项卡中执行此操作,并设置“命令行参数”字段。这将允许您调试应用程序,就像在IDE外部运行时从提示中指定了命令行一样。通常,在尝试使用它之前,应始终检查是否存在参数;

if (args != null && args.Length > 1)
{
    // now you know there's thing in "args[0]"
}

答案 4 :(得分:1)

args[0]包含启动应用程序时传递的第一个命令行参数。

答案 5 :(得分:1)

args [0]是执行过程中传递的第一个参数,即

  

C:\程序的路径\ Program.exe&#34;您的目录路径&#34;