C # traverse all objects in a specified folder

Source: Internet
Author: User

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 );
}

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.