Home > Net >  How to judge whether the folder is empty?
How to judge whether the folder is empty?

Time:09-15

Determine the folder with the following code is empty:

String newPath="Fenmo.zip";
String [] files=Directory. GetFiles (newPath);
If (files==null | | files. The length==0)//this is 58 words
TextBox1. Text="folder is empty!" ;

Compile an exception:
Severity code shows the project file line prohibits display status
Error CS1061 "string []" does not contain the definition of "length", and couldn't find an acceptable first "string []" type parameters can access extension method "length" (the lack of a using directive or assembly references?) WindowsFormsApp1 F: \ c # 8 file length measurement practice \ \ WindowsFormsApp1 \ WindowsFormsApp1 \ Form1 cs 58 activity

Please give advice or comments

CodePudding user response:

Length in the first letter should be capitalized?

CodePudding user response:

Pay attention to the case,

CodePudding user response:

If (files==null | | files. The Count ()==0)

CodePudding user response:

Try this,
//pbak is to check whether the empty folder path 
If (Directory. GetDirectories (pbak). Length & gt; 0 | | Directory. GetFiles (pbak) Length & gt; 0)
{
Response. Write (" & lt; script> Alert (' folder is not empty! '); </script>" );
}

CodePudding user response:

if (! Files? .length & gt; 0)

CodePudding user response:

To use LINQ in acme, ha ha, this MAO a thing, does not, consult the directory, check the number of files and directories,
The last judgment, if the three baidu not to come out, that still don't learn c #, ha ha, personal meaning, don't agree with your reply

CodePudding user response:

Int count=Directory. GetFiles (newPath) count;

CodePudding user response:

DirectoryInfo dir=new DirectoryInfo (newPath);
The FileInfo [] finfo=dir. GetFiles ();
If (finfo. The Count ()==0)
TextBox1. Text="folder is empty!" ;

CodePudding user response:

Why use the Count and Length?
Any is not working

CodePudding user response:

Files. Any ()

CodePudding user response:

1. The first determines whether the path there
2. Then determine whether it contains folders or include file, as shown in example: example of a judge whether a folder is empty

CodePudding user response:

The second sentence if I remember correctly should not pass parameters, so that access to all files folder, the incoming string as specified under the filter conditions, such as the file name extension.
  •  Tags:  
  • C#
  • Related