2

这段代码在这个例子中做了什么,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();

                }


        }
    }
4

6 回答 6

3

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

它的命令行参数,如果你从命令行执行你的参数,你会得到它的价值exe

出于调试目的,您也可以通过 Visual Studio 发送它,转到项目属性、调试并在启动选项下指定。

在此处输入图像描述

于 2013-03-04T12:45:09.273 回答
2

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

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

System.Reflection.Assembly.GetExecutingAssembly().Location

另请参阅此问题:如何在 .NET 控制台应用程序中获取应用程序的路径?

于 2013-03-04T12:53:32.793 回答
2

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

MyProgram.exe

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

MyProgram.exe Hello

这将传递Helloargs[],因此 index 处的值0将是Hello。现在让我们考虑这个命令行:

MyProgram.exe Hello World

这将传递HelloWorld进入args[],因此 index 处的值0将是Hello并且 index 处的值1将是World。现在,要记住的另一件事是"参数,尤其是在必须处理路径时:

MyProgram.exe "C:\MyPath\ToSomewhere"
于 2013-03-04T12:48:01.470 回答
1

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

if (args != null && args.Length > 1)
{
    // now you know there's thing in "args[0]"
}
于 2013-03-04T12:45:37.663 回答
1

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

于 2013-03-04T12:46:50.157 回答
1

args[0] 是执行期间传递的第一个参数,即

C:\Path to your program\Program.exe "你的目录路径"

于 2013-03-04T12:47:34.180 回答