using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ListDirectoryAndFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonGetPath_Click(object sender, EventArgs e)
{
String []path = { txtBoxFilePath.Text };
TestPath(path);
}
//start
public void TestPath(string []testOne ) // msdn main()
{
foreach (string path in testOne )
{
if (File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if (Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
MessageBox.Show( path + " is not a valid fiel or directory " );
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public 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);
}
// Insert logic for processing found files here.
public void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
txtBoxFileAndDir.Text += path + "/r/n";
}
//end
}
}