C# 在C语言中获取数组外部的索引#

C# 在C语言中获取数组外部的索引#,c#,C#,这个程序应该显示一个目录的路径,如果目录存在,那么它还应该显示里面有以下扩展名的文件(例如.doc、.pdf、.jpg、.jpeg),但是我得到了一个错误 *索引超出了数组的边界 在这行代码上 string directoryPath = args[0]; 这是主函数中的代码 class Program { static void Main(string[] args) { string directoryPath =

这个程序应该显示一个目录的路径,如果目录存在,那么它还应该显示里面有以下扩展名的文件(例如.doc、.pdf、.jpg、.jpeg),但是我得到了一个错误

*索引超出了数组的边界

在这行代码上

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.");

                }


        }
    }
类程序
{
静态void Main(字符串[]参数)
{
字符串directoryPath=args[0];
字符串[]filesList,filesListTmp;
IFileOperation[]opList={new FileProcNameAfter10(),
新建FileProcEnc(),
新文件ProcByExt(“jpeg”),
新文件ProcByExt(“jpg”),
新文件ProcByExt(“doc”),
新文件ProcByExt(“pdf”),
新文件ProcByExt(“djvu”)
};
if(Directory.Exists(directoryPath))
{
filesList=Directory.GetFiles(directoryPath);
while(true)
{
睡眠(500);
filelistmp=Directory.GetFiles(directoryPath);
foreach(可枚举中的var元素。除了(filelistmp,filelist))
{
控制台写入线(elem);
foreach(opList中的var op)
{
如果(操作接受(要素))操作过程(要素);
}
}
filesList=filesListTmp;
if(Console.KeyAvailable==true&&Console.ReadKey(true.Key==ConsoleKey.Escape)中断;
}
}
其他的
{
WriteLine(“没有这样的目录”);
}
}
}

如何处理此错误这似乎很常见,但它以不同的方式发生

运行程序时,需要将必要的参数传递给程序。您可以通过从命令行运行程序来执行此操作,也可以在运行Visual Studio时执行以下操作:

  • 右键单击项目
  • 性质
  • 调试标签
  • 在“开始选项->命令行参数”下输入参数

  • 运行程序时,需要将必要的参数传递给程序。您可以通过从命令行运行程序来执行此操作,也可以在运行Visual Studio时执行以下操作:

  • 右键单击项目
  • 性质
  • 调试标签
  • 在“开始选项->命令行参数”下输入参数

  • 您可能希望从命令行将参数传递到程序中

    像这样:

    >yourProgram.exe目录名

    此外,为了避免代码中出现此类问题

    if(args.Length > 0){
      string directoryPath = args[0];
    }else{
      //print a help message and exit, or do something like set the 
      //default directoryPath to current directory
    }
    

    您可能希望从命令行将参数传递到程序中

    像这样:

    >yourProgram.exe目录名

    此外,为了避免代码中出现此类问题

    if(args.Length > 0){
      string directoryPath = args[0];
    }else{
      //print a help message and exit, or do something like set the 
      //default directoryPath to current directory
    }
    

    您希望用户在程序启动时输入路径还是在他们启动程序时输入路径?如果是第一个,那么应该添加一个Console.Read()方法来请求路径

    如果是后者,则在启动程序时需要将路径作为参数传递。在读取args数组之前,还应该检查它是否包含数据以及数据是否为有效路径

    比如:

     if(args.Length > 0 && Directory.Exists(args[0]))
     {
       // Do Something.
     }
    

    您希望用户在程序启动时输入路径还是在他们启动程序时输入路径?如果是第一个,那么应该添加一个Console.Read()方法来请求路径

    如果是后者,则在启动程序时需要将路径作为参数传递。在读取args数组之前,还应该检查它是否包含数据以及数据是否为有效路径

    比如:

     if(args.Length > 0 && Directory.Exists(args[0]))
     {
       // Do Something.
     }
    

    运行应用程序时是否将目录作为参数传递运行应用程序时是否将目录作为参数传递对不起,我弄丢了应该在那里键入什么?应该输入什么参数?仅输入目录的路径,在你的情况下,我很抱歉,但是我弄丢了我到底应该在那里键入什么我应该输入什么参数??在你的情况下,只是目录的路径。