C# 文件夹 路径 操作

C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
//遍历文件夹
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach(FileInfo NextFile in TheFolder.GetFiles())
this.listBox2.Items.Add(NextFile.Name);
===================================================================
如何获取指定目录包含的文件和子目录
1. DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找;
2. DirectoryInfo.GetDirectories():获取目录(不包含子目录)的子目录,返回类型为DirectoryInfo[],支持通配符查找;
3. DirectoryInfo. GetFileSystemInfos():获取指定目录下(不包含子目录)的文件和子目录,返回类型为FileSystemInfo[],支持通配符查找;
如何获取指定文件的基本信息;
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;

protected void Button1_Click(object sender, EventArgs e)
     {

         if (Directory.Exists(Server.MapPath("~/upimg/hufu")) == false)//如果不存在就创建file文件夹
         {
             Directory.CreateDirectory(Server.MapPath("~/upimg/hufu"));
         }

         //Directory.Delete(Server.MapPath("~/upimg/hufu"), true);//删除文件夹以及文件夹中的子目录,文件   

         //判断文件的存在

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

             //存在文件

         }

         else
         {
             Response.Write("No");
             //不存在文件
             File.Create(MapPath("~/upimg/Data.html"));//创建该文件

         }

         string name = GetFiles.FileName;//获取已上传文件的名字
         string size = GetFiles.PostedFile.ContentLength.ToString();//获取已上传文件的大小
         string type = GetFiles.PostedFile.ContentType;//获取已上传文件的MIME
         string postfix = name.Substring(name.LastIndexOf(".") + 1);//获取已上传文件的后缀
         string ipath = Server.MapPath("upimg") +"\"+ name;//获取文件的实际路径
         string fpath = Server.MapPath("upfile") + "\" + name;
         string dpath = "upimg\" + name;//判断写入数据库的虚拟路径

         ShowPic.Visible = true;//激活
         ShowText.Visible = true;//激活

         //判断文件格式

猜你喜欢

转载自blog.csdn.net/lm393485/article/details/88894291