访问索引c#中的数组值

时间:2014-11-13 17:02:48

标签: c# arrays loops for-loop indexing

我正在编写一些c#代码,我已经碰到了关于如何在两个开始和结束索引之间访问文件的问题,

我编写了一个简短的程序来过滤掉某个目录中的文件,将文件名添加到数组中,然后识别该文件中的Start文件和End文件的索引。

现在我想打印到从Start索引到End索引的控制台,我知道它应该很简单,但我想我需要一个巧克力棒!我撞墙了,

class Program
{

    static void Main()
    {
        string WorkDir;

        Console.WriteLine("Right click-Paste working directory to here and enter:");
        Console.WriteLine();

        WorkDir = Console.ReadLine();

        // Put all file names in root directory into array.
        string WorkingDirectory = (WorkDir);


        //String FileFilterNumber1 = ("00 DEV1*.csv");
        //var FileFilter = 0;

        Console.WriteLine();
        Console.WriteLine("Type 1,2,3 or 4 for Devn filter respectively");
        Console.WriteLine();

        string DevSwitch = Console.ReadLine();

        string DevFilter = "nn DEVn 2????????????????????.csv";    // initialised to standard template only at this point.
        //Console.WriteLine("{0}", DevFilter);

        switch (DevSwitch)
        {
            case "1":
                DevFilter = ("00 DEV1 2????????????????????.csv");
                Console.WriteLine("Directory search filter to be applied is {0}", DevFilter);
                break;

            case "2":
                DevFilter = ("01 DEV2 2????????????????????.csv");
                Console.WriteLine("Directory search filter to be applied is {0}", DevFilter);
                break;

            case "3":
                DevFilter = ("02 DEV3 2????????????????????.csv");
                Console.WriteLine("Directory search filter to be applied is {0}", DevFilter);
                break;


            case "4":
                DevFilter = ("03 DEV4 2????????????????????.csv");
                Console.WriteLine("Directory search filter to be applied is {0}", DevFilter);
                break;

            default:
                Console.WriteLine("Invalid selection. Please close window");
                break;

        }
        //Console.WriteLine("{0}", DevFilter);
        //Console.WriteLine("{0}", DevFilter);
        Console.WriteLine();

        Console.WriteLine("Enter Start date of test in the format yyyy-mm-dd (As per filename format)");
        Console.WriteLine();

        string TestStart;

        TestStart = Console.ReadLine();

        Console.WriteLine();
        Console.WriteLine("Enter End date of test in the format yyyy-mm-dd (As per filename format)");
        Console.WriteLine();

        string TestEnd;

        TestEnd = Console.ReadLine();
        Console.WriteLine();

        string[] DirFileArray = Directory.GetFiles(WorkingDirectory, DevFilter); //excludes "repaired" files from array, and "Averaged" logs, if found, note: does not exclude duplicate files if they exist (yet)

        //Console.WriteLine("--- Start of test date found at index: ---");
        {
            string findStartString = (TestStart);
            int strStartNumber;
            int strStartIndex;
            for (strStartNumber = 0; strStartNumber < DirFileArray.Length; strStartNumber++)
            {
                strStartIndex = DirFileArray[strStartNumber].IndexOf(findStartString);
                if (strStartIndex >= 0)
                    break;
            }
            Console.WriteLine();
            Console.WriteLine("--- Start of test date found at file number: {0}", strStartNumber);
            Console.WriteLine();

            //Console.WriteLine("String number: {0}\nString index: {1}",
            //   strNumber, strIndex);

            Console.WriteLine(" The filename found for the start of the survey is {0} ", DirFileArray[strStartNumber]);
            Console.WriteLine();

            string findEndString = (TestEnd);
            int strEndNumber;
            int strEndIndex;
            for (strEndNumber = 0; strEndNumber < DirFileArray.Length; strEndNumber++)
            {
                strEndIndex = DirFileArray[strEndNumber].IndexOf(findEndString);
                if (strEndIndex >= 0)
                    break;
            }
            Console.WriteLine();
            Console.WriteLine("--- End of test date found at file number: {0}", strEndNumber);
            Console.WriteLine();
            //Console.WriteLine("String number: {0}\nString index: {1}",
            //   strNumber, strIndex);

            Console.WriteLine(" The filename found for the end of the survey is {0} ", DirFileArray[strEndNumber]);

            // write out contents of DirFileArray to screen between 

            //int DirFileSpan;

            //DirFileSpan = (strEndNumber - strStartNumber); // uses this method as strStartNumber will often than zero as junk in front of start date.
            //Console.WriteLine(DirFileSpan);

            Console.ReadKey();

        }

        Console.ReadLine();
    }
}

0 个答案:

没有答案