c# I cannot get my code to sum file counts for directories beyond the root level

StackOverflow https://stackoverflow.com/questions/23583902

  •  19-07-2023
  •  | 
  •  

문제

I'm using 2008 and am unable to use the EnumerateFiles class.

Any folder beyond the root level are essentially ignored when attempting to sum the files within listed network path. Here's my code:

    public void getLeverageServer(string Server)
    {
        int Inp = 0;
        int Out = 0;
        int Ex = 0;

        string output = "\\\\" + Server + "\\F\\Output";
        string input = "\\\\" + Server + "\\F\\Input";
        string exceptions = "\\\\" + Server + "\\F\\Exceptions";

        string[] pathIn = Directory.GetFiles(input);
        string[] pathOut = Directory.GetFiles(output);
        string[] pathExceptions = Directory.GetFiles(exceptions);

        foreach (string element in pathIn)
        {
            Inp++;
        }

        foreach (string element in pathOut)
        {
            Out++;
        }

        foreach (string element in pathExceptions)
        {
            Ex++;
        }

        txtLevInp.Text = Convert.ToString(Inp);
        txtLevOut.Text = Convert.ToString(Out);
        txtLevExc.Text = Convert.ToString(Ex);
        txtLevTotal.Text = Convert.ToString(Out + Ex);
    }
도움이 되었습니까?

해결책

You need a different overload of Directory.GetFiles

string[] pathIn = Directory.GetFiles(input, "*.*", SearchOption.AllDirectories);

And you don't need to count the files found. Just read the value of the property Length of the returned array....

txtLevInp.Text = pathIn.Length;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top