C # traverse all objects in a specified folder
Directoryinfo thefolder = new directoryinfo (folderfullname );
// Traverse folders
Foreach (directoryinfo nextfolder in thefolder. getdirectories ())
This. listbox1.items. Add (nextfolder. Name );
// Traverse objects
Foreach (fileinfo nextfile in thefolder. getfiles ())
This. listbox2.items. Add (nextfile. Name );
========================================================== ======================================
How to obtain files and subdirectories in a specified directory
1. directoryinfo. getfiles (): Get the files in the directory (excluding subdirectories). The returned type is fileinfo []. wildcard search is supported;
2. directoryinfo. getdirectories (): Obtain the subdirectory of the directory (excluding the subdirectory). The return type is directoryinfo []. Wildcards are supported;
3. directoryinfo. getfilesysteminfos (): gets the files and subdirectories under the specified directory (excluding subdirectories). The return type is filesysteminfo []. Wildcards are supported;
How to obtain the basic information of a specified object;
Fileinfo. exists: obtains whether the specified file exists;
Fileinfo. Name, fileinfo. extensioin: get the file name and extension;
Fileinfo. fullname: Get the full-qualified name of the file (full path );
Fileinfo. Directory: directory where the file is located. The returned type is directoryinfo;
Fileinfo. directoryname: Obtain the path of the directory where the file is located (full path );
Fileinfo. Length: get the file size (number of bytes );
Fileinfo. isreadonly: Gets whether the file is read-only;
Fileinfo. attributes: gets or sets the attributes of a specified file. The returned type is fileattributes enumeration, which can be a combination of multiple values.
Fileinfo. creationtime, fileinfo. lastaccesstime, and fileinfo. lastwritetime are used to obtain the file creation time, access time, and modification time respectively;
Assume that the folder a is under drive F,CodeAs follows. Output the file name to a ListBox.
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Namespace windowsformsapplication1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button2_click (Object sender, eventargs E)
{
Directoryinfo thefolder = new directoryinfo (@ "F: \ \");
Directoryinfo [] dirinfo = thefolder. getdirectories ();
// Traverse folders
Foreach (directoryinfo nextfolder in dirinfo)
{
// This. listbox1.items. Add (nextfolder. Name );
Fileinfo [] fileinfo = nextfolder. getfiles ();
Foreach (fileinfo nextfile in fileinfo) // traverses the file
This. listbox2.items. Add (nextfile. Name );
}
}
}
}
C # traverse all objects in a specified folder
Directoryinfo thefolder = new directoryinfo (folderfullname );
// Traverse folders
Foreach (directoryinfo nextfolder in thefolder. getdirectories ())
This. listbox1.items. Add (nextfolder. Name );
// Traverse objects
Foreach (fileinfo nextfile in thefolder. getfiles ())
This. listbox2.items. Add (nextfile. Name );
========================================================== ======================================
How to obtain files and subdirectories in a specified directory
1. directoryinfo. getfiles (): Get the files in the directory (excluding subdirectories). The returned type is fileinfo []. wildcard search is supported;
2. directoryinfo. getdirectories (): Obtain the subdirectory of the directory (excluding the subdirectory). The return type is directoryinfo []. Wildcards are supported;
3. directoryinfo. getfilesysteminfos (): gets the files and subdirectories under the specified directory (excluding subdirectories). The return type is filesysteminfo []. Wildcards are supported;
How to obtain the basic information of a specified object;
Fileinfo. exists: obtains whether the specified file exists;
Fileinfo. Name, fileinfo. extensioin: get the file name and extension;
Fileinfo. fullname: Get the full-qualified name of the file (full path );
Fileinfo. Directory: directory where the file is located. The returned type is directoryinfo;
Fileinfo. directoryname: Obtain the path of the directory where the file is located (full path );
Fileinfo. Length: get the file size (number of bytes );
Fileinfo. isreadonly: Gets whether the file is read-only;
Fileinfo. attributes: gets or sets the attributes of a specified file. The returned type is fileattributes enumeration, which can be a combination of multiple values.
Fileinfo. creationtime, fileinfo. lastaccesstime, and fileinfo. lastwritetime are used to obtain the file creation time, access time, and modification time respectively;
Assume that the folder is in drive F, and the code is as follows. Output the file name to a ListBox.
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Namespace windowsformsapplication1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void button2_click (Object sender, eventargs E)
{
Directoryinfo thefolder = new directoryinfo (@ "F: \ \");
Directoryinfo [] dirinfo = thefolder. getdirectories ();
// Traverse folders
Foreach (directoryinfo nextfolder in dirinfo)
{
// This. listbox1.items. Add (nextfolder. Name );
Fileinfo [] fileinfo = nextfolder. getfiles ();
Foreach (fileinfo nextfile in fileinfo) // traverses the file
This. listbox2.items. Add (nextfile. Name );
}
}
}
}