locked
count number of files in c#

    Question

  • hi all

    i am trying to find a way to count the number of files in a particular folder.

    i have done the same very easily in visual basic 6

    but as i am creating a similar application in c# i am stuck up with this..

    can anyone please help me with a code snippet or provide me some kind of assistance..

    thank you.

    it would be really greatful if someone helps me

    Tuesday, February 13, 2007 3:58 AM

Answers

  • This is easier in .NET...

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\");
                int count = dir.GetFiles().Length;
    Tuesday, February 13, 2007 4:16 AM

All replies

  • This is easier in .NET...

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\");
                int count = dir.GetFiles().Length;
    Tuesday, February 13, 2007 4:16 AM
  • thank  u Mr.Leon..

    I will try this out and reply u.

    thank u

    Thursday, February 15, 2007 8:42 AM
  • thank you Mr.Leon..

    It  worked

    Thanks a lot

    Thursday, February 15, 2007 8:46 AM
  • no problem... glad to help! ;-)
    Thursday, February 15, 2007 9:02 AM
  • Hi Leon,

    I was searching for the same on how to count the number of files in a particular folder on my machine. The piece of code did worked. Thanks a lot.

    Could you please suggest on how do count a number of files in a particular folder on Remote Machine ?

    I tried the below code(just updating few lines):

                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("\\\\RemoteMachineName\\foldername");
                int count = dir.GetFiles().Length;

    But threw an exception as "The network path was not found"

    Could you please help me on this.

    Thank you

    Wednesday, May 02, 2012 12:38 PM
  • Hi Sridhar,

    I do not know if you are still working on "The network path was not found" exception. If you are, while specifying the drive on the remote machine you may want give it shot by using '$' in place of ':' . E.g. @"\\RemoteMachineName\C$\foldername"  instead of @"\\RemoteMachineName\C:\foldername"

    Friday, August 03, 2012 7:32 PM