获得并处理一个文件夹下的所有文件
2007-10-17 09:41
204 查看
.netFrameWork给我们提供了很多良好的文件操作类。
其中有一个地址操作类叫做Directory,其是一个静态类,使用这个类,将节省实例化的时间。
要获得一个文件夹下的所有文件,大约有两种方式:
(1)直接调用Directory类的静态方法。 string[] Directory.GetFiles(三个参数);其中第三个参数确定是否包含子目录
GetFiles(string path, string searchPattern, SearchOption searchOption)#region GetFiles(string path, string searchPattern, SearchOption searchOption)
//
// Summary:
// Returns the names of files in the specified directory that match the specified
// search pattern, using a value to determine whether to search subdirectories.
//
// Parameters:
// searchOption:
// One of the System.IO.SearchOption values that specifies whether the search
// operation should include all subdirectories or only the current directory.
//
// path:
// The directory to search.
//
// searchPattern:
// The search string to match against the names of files in path. The parameter
// cannot end in two periods ("..") or contain two periods ("..") followed by
// System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar,
// nor can it contain any of the characters in System.IO.Path.InvalidPathChars.
//
// Returns:
// A String array containing the names of files in the specified directory that
// match the specified search pattern. File names include the full path.
//
// Exceptions:
// System.UnauthorizedAccessException:
// The caller does not have the required permission.
//
// System.ArgumentNullException:
// path or searchpattern is null.
//
// System.IO.IOException:
// path is a file name.
//
// System.ArgumentException:
// path is a zero-length string, contains only white space, or contains one
// or more invalid characters as defined by System.IO.Path.InvalidPathChars.
// -or- searchPattern does not contain a valid pattern.
//
// System.IO.PathTooLongException:
// The specified path, file name, or both exceed the system-defined maximum
// length. For example, on Windows-based platforms, paths must be less than
// 248 characters and file names must be less than 260 characters.
//
// System.IO.DirectoryNotFoundException:
// The specified path is invalid (for example, it is on an unmapped drive).
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption);
#endregion
(2)使用递归程序:找到其中的所有的子文件夹,子文件夹再调用函数本身获得其中的文件名。
使用两个函数string[] Directory.GetFiles()//三个参数,这里空着第三个参数,
和获得 子目录的函数 public static string[] GetDirectories(string path);
其中有一个地址操作类叫做Directory,其是一个静态类,使用这个类,将节省实例化的时间。
要获得一个文件夹下的所有文件,大约有两种方式:
(1)直接调用Directory类的静态方法。 string[] Directory.GetFiles(三个参数);其中第三个参数确定是否包含子目录
GetFiles(string path, string searchPattern, SearchOption searchOption)#region GetFiles(string path, string searchPattern, SearchOption searchOption)
//
// Summary:
// Returns the names of files in the specified directory that match the specified
// search pattern, using a value to determine whether to search subdirectories.
//
// Parameters:
// searchOption:
// One of the System.IO.SearchOption values that specifies whether the search
// operation should include all subdirectories or only the current directory.
//
// path:
// The directory to search.
//
// searchPattern:
// The search string to match against the names of files in path. The parameter
// cannot end in two periods ("..") or contain two periods ("..") followed by
// System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar,
// nor can it contain any of the characters in System.IO.Path.InvalidPathChars.
//
// Returns:
// A String array containing the names of files in the specified directory that
// match the specified search pattern. File names include the full path.
//
// Exceptions:
// System.UnauthorizedAccessException:
// The caller does not have the required permission.
//
// System.ArgumentNullException:
// path or searchpattern is null.
//
// System.IO.IOException:
// path is a file name.
//
// System.ArgumentException:
// path is a zero-length string, contains only white space, or contains one
// or more invalid characters as defined by System.IO.Path.InvalidPathChars.
// -or- searchPattern does not contain a valid pattern.
//
// System.IO.PathTooLongException:
// The specified path, file name, or both exceed the system-defined maximum
// length. For example, on Windows-based platforms, paths must be less than
// 248 characters and file names must be less than 260 characters.
//
// System.IO.DirectoryNotFoundException:
// The specified path is invalid (for example, it is on an unmapped drive).
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption);
#endregion
(2)使用递归程序:找到其中的所有的子文件夹,子文件夹再调用函数本身获得其中的文件名。
使用两个函数string[] Directory.GetFiles()//三个参数,这里空着第三个参数,
和获得 子目录的函数 public static string[] GetDirectories(string path);
相关文章推荐
- 批处理,用一个exe处理一个文件夹下的所有txt文件
- java 如何递归遍历一个文件夹下的所有文件
- 获取一个想要的指定文件的集合,获取文件夹下(包含子目录的所有.java的文件对象,并存储到集合中)
- PHP读某一个目录下所有文件和文件夹
- php--------删除一个路径下的所有文件夹和文件
- 读取一个文件夹下的所有文件
- 删除一个目录下所有文件及文件夹 BOOL DeleteDirectory(LPCTSTR pszPath)
- C#控制台控制台将一个文件夹中的所有txt文件中的数据读取出来
- 黑马程序员—这是一个能够键盘录入路径去批量修改指定文件夹下所有指定文件内容的方法
- 编写一个SHELL脚本,可以获得指定目录下的所有文件及文件夹的大小。输出的样式与 ls -lh 命令相同。
- 获得文件夹中的所有文件类
- Python OS导入一个文件夹所有文件
- C语言 实现遍历一个文件夹的所有文件
- asp.net 得到一个文件夹下的所有文件夹及子文件夹名,得到所有文件名,文件大小,文件夹大小
- 利用java语言获得某个文件夹下符合条件的所有文件
- 获取一个文件夹下的所有文件
- 如何让文件夹内所有文件名显示在一个Excel文件
- JAVA遍历一个文件夹中的所有文件的小例子
- 更新一个文件夹下所有的文件