Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This may be a stupid question but im trying to find out how many files are in a folder and one minuite its telling me 0 and the next time its telling me 1 when there is simply no files there.

I even made a new folder called H in my documents with nothing in there at all and its still coming back 1 item. someone please explain this or even try it yourself, its hurting my head

C#
int i = Directory.GetFiles(@"C:\Users\William\Documents\H\").Length;
            MessageBox.Show(Convert.ToString(i));
Posted
Comments
Kevin Marois 19-Aug-15 19:05pm    
Any hidden files in there? What is the name of the file is says is there? Does File.Exist() show it there?
Member 11921461 19-Aug-15 19:12pm    
no hidden files, im not sure how to check the other things, im not that experienced with programming
Kevin Marois 19-Aug-15 19:13pm    
if(File.Exists(someFileName))
{
// The file is there
}
Member 11921461 19-Aug-15 19:14pm    
but what file name would i use? theres no files in there to check
Kevin Marois 19-Aug-15 19:16pm    
Directory.getFiles loads an array with the names of any files that are in the directory. See this:
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx

Try
C#
if(Directory.Exists(path))
            {
                // This path is a directory
              int i = Directory.GetFiles(path).Length;
              MessageBox.Show(Convert.ToString(i));
                   
            }
 
Share this answer
 
if Directory.GetFiles return one as length, at that time there may be a file. So why don't you debug and check the file details at run time or show message with file names?
do as below
C#
string[] files= Directory.GetFiles(@"C:\Users\William\Documents\H\");
MessageBox.Show(string.Format("File Count :{0} and Files :{1}", files.Length, string.Join(",",  files)));
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900