C# folder path operation

C# Traverse all files in the specified folder
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
//Traverse the folder
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
this.listBox1.Items.Add(NextFolder.Name);
//Traverse File
foreach(FileInfo NextFile in TheFolder.GetFiles())
this.listBox2.Items.Add(NextFile.Name);
========================= =========================================
How to get the files and sub-files contained in the specified directory Directory
1. DirectoryInfo.GetFiles(): Get the files in the directory (excluding subdirectories), the return type is FileInfo[], support wildcard search;
2. DirectoryInfo.GetDirectories(): Get the subdirectories of the directory (excluding subdirectories) Directory, the return type is DirectoryInfo[], supports wildcard search;
3. DirectoryInfo. GetFileSystemInfos(): Gets files and subdirectories in the specified directory (excluding subdirectories), the return type is FileSystemInfo[], supports wildcard search;
How to get the basic information of the specified file;
FileInfo.Exists: get whether the specified file exists;
FileInfo.Name, FileInfo.Extensioin: get the name and extension of the
file ; FileInfo.FullName: get the fully qualified name (full path) of the file;
FileInfo .Directory: Get the directory where the file is located, and the return type is DirectoryInfo;
FileInfo.DirectoryName: Get the path (full path) of the directory where the file is located;
FileInfo.Length: Get the size of the file (in bytes);
FileInfo.IsReadOnly: Get whether the file is only Read;
FileInfo.Attributes: get or set the attributes of the specified file, the return type is FileAttributes enumeration, which can be a combination of multiple values
FileInfo.CreationTime, FileInfo.LastAccessTime, FileInfo.LastWriteTime: used to obtain the creation time and access of the file respectively Time, modification time;

protected void Button1_Click(object sender, EventArgs e)
     {

         if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//If it does not exist, create a file folder
         {
             Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
         }

         //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//Delete folders and folders The subdirectory of the file   

         //Judging the existence of the file

         if (File.Exists(Server.MapPath("~/upimg/Data.html")))
         {
             Response.Write("Yes");

             //File exists

         }

         else
         {
             Response.Write("No");
             //The file does not exist
             File.Create(MapPath("~/upimg/Data.html"));//Create the file

         }

         string name = GetFiles.FileName;//Get uploaded File name
         string size = GetFiles.PostedFile.ContentLength.ToString();//Get the size of the uploaded file
         string type = GetFiles.PostedFile.ContentType;//Get the MIME
         string of the uploaded file postfix = name.Substring(name.LastIndexOf(" .") + 1);//Get the suffix of the uploaded file
         string ipath = Server.MapPath("upimg") +"\"+ name;//Get the actual path of the file
         string fpath = Server.MapPath("upfile" ) + "\" + name;
         string dpath = "upimg\" + name;//Judging the virtual path written to the database

         ShowPic.Visible = true;//Activating
         ShowText.Visible = true;//Activating

         //Judging the file format

Guess you like

Origin blog.csdn.net/lm393485/article/details/88894291