using System;
using System.IO;
using System.Collections;
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static void Main(string[] args) {
foreach(string path in args) {
if(File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static void ProcessDirectory(string targetDirectory) {
// Process the list of files found in the directory
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName); // Recurse into subdirectories of this directory
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Real logic for processing found files would go here.
public static void ProcessFile(string path) {
Console.WriteLine("Processed file '{0}'.", path);
}
}
[JScript]
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;// For File.Exists, Directory.Exists
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static function Main(args : String[]) : void {
for(var i : int in args) {
var path : String = args[i];
if (File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static function ProcessDirectory(targetDirectory : String) : void {
// Process the list of files found in the directory
var fileEntries : String [] = Directory.GetFiles(targetDirectory);
for (var i : int in fileEntries)
ProcessFile(fileEntries[i]); // Recurse into subdirectories of this directory
var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
for (i in subdirectoryEntries)
ProcessDirectory(subdirectoryEntries[i]);
}
// Real logic for processing found files would go here.
public static function ProcessFile(path : String) : void {
Console.WriteLine("Processed file '{0}'.", path);
}
}// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());
[C++] 没有可用于 C++ 的示例。若要查看 Visual Basic、C# 或 JScript 示例,请单击页左上角的语言筛选器按钮 。
using System.IO;
using System.Collections;
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static void Main(string[] args) {
foreach(string path in args) {
if(File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static void ProcessDirectory(string targetDirectory) {
// Process the list of files found in the directory
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName); // Recurse into subdirectories of this directory
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Real logic for processing found files would go here.
public static void ProcessFile(string path) {
Console.WriteLine("Processed file '{0}'.", path);
}
}
[JScript]
//For Directory.GetFiles and Directory.GetDirectories
import System;
import System.IO;
import System.Collections;// For File.Exists, Directory.Exists
// Takes an array of file names or directory names on the command line.
// Determines what kind of name it is and processes it appropriately
public class RecursiveFileProcessor {
public static function Main(args : String[]) : void {
for(var i : int in args) {
var path : String = args[i];
if (File.Exists(path)) {
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path)) {
// This path is a directory
ProcessDirectory(path);
}
else {
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, and recurse on any directories
// that are found to process the files they contain
public static function ProcessDirectory(targetDirectory : String) : void {
// Process the list of files found in the directory
var fileEntries : String [] = Directory.GetFiles(targetDirectory);
for (var i : int in fileEntries)
ProcessFile(fileEntries[i]); // Recurse into subdirectories of this directory
var subdirectoryEntries : String[] = Directory.GetDirectories(targetDirectory);
for (i in subdirectoryEntries)
ProcessDirectory(subdirectoryEntries[i]);
}
// Real logic for processing found files would go here.
public static function ProcessFile(path : String) : void {
Console.WriteLine("Processed file '{0}'.", path);
}
}// For JScript there is no 'Main' routine defined and hence the command line arguments
// have to be obtained with a call to System.Environment.GetCommandLineArgs
RecursiveFileProcessor.Main(System.Environment.GetCommandLineArgs());
[C++] 没有可用于 C++ 的示例。若要查看 Visual Basic、C# 或 JScript 示例,请单击页左上角的语言筛选器按钮 。
if(MyDirectory.Exists)
{
MessageBox.Show("存在");
}
else
{
MessageBox.Show("不存在"); }
if f exist即可
......
if(Directory.Exists(@"d:\winnt")) Console.WriteLine("dir exists.");
else Console.WriteLine("dir not exists.");
if(File.Exists(@"D:\WINNT\system32\cmd.exe")) Console.WriteLine("file exists.");
else Console.WriteLine("file not exists.");
是静态方法,不必生成实例
foreach(string d in Directory.GetDirectories(@"D:\WINNT")) Console.WriteLine(d);//文件
foreach(string f in Directory.GetFiles(@"D:\WINNT")) Console.WriteLine(f);