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