用博客记录点滴……

C#的一些小知识

一、Server.MapPath

E:\MyProject\GisSystem\Json\jsonlist.aspx,GisSystem项目下有个Json文件夹,文件夹下有个jsonlist.aspx。
运行jsonlist.aspx时:
Server.MapPath("")//当前运行文件所在的目录,E:\MyProject\GisSystem\Json
Server.MapPath("./")//当前运行文件所在的目录,E:\MyProject\GisSystem\Json\
Server.MapPath("../")//当前运行文件所在目录的父级目录,E:\MyProject\GisSystem\
Server.MapPath("~/")//当前项目所在的根目录,E:\MyProject\GisSystem\

 

二、System.IO.Path

(1)System.IO.Path.GetDirectoryName(FileName) 返回路经 。
如:GetDirectoryName("c:\test\tmp.txt") 返回 c:\test
(2)System.IO.Path.GetFileName(FileName) 返回不包含路的文件名。
如: GetFileName("c:\test\tmp.txt") 返回 tmp.txt
(3) System.IO.Path.GetExtension(FileName) 返回 后缀名。
如 :GetExtension("c:\test\tmp.txt")返回.txt

 

三、获取一个文件夹中的指定后缀名文件

 1 protected  List<string> GetFileFromDic(string filepath, string extension)
 2         {
 3             List<string> pList = new List<string>();
 4             try
 5             {
 6                 DirectoryInfo theFolder = new DirectoryInfo(filepath);
 7                 FileInfo[] fileInfo = theFolder.GetFiles();
 8                 foreach (FileInfo fInfo in fileInfo)
 9                 {
10                     if (fInfo.Extension.Contains(extension))
11                     {
12                         pList.Add(fInfo.Name);//包括文件名和扩展名
13                     }
14                 }
15             }
16             catch
17             {
18             }
19             return pList;
20         }

 

 

 

 

posted @ 2014-02-26 09:58  aegisada  阅读(274)  评论(0编辑  收藏  举报