//CSDN看到的,放在这里先,有时间研究一下^_^
using System;
using System.Collections;
using System.IO;
namespace ZZ
{
class ZZTest
{
static void Main()
{
string [] files = GetFiles("G://back");
foreach(string file in files)
{
Console.WriteLine(file);
}
Console.ReadLine();
}
public static string[] GetFiles(string path)
{
ArrayList files = new ArrayList();
//获取当前目录下的文件
foreach(string file in Directory.GetFiles(path))
{
files.Add(file);
}
//获取当前目录下的子目录文件
foreach(string dir in Directory.GetDirectories(path))
{
GetFiles(dir,files);
}
return (string [])files.ToArray(typeof(string));
}
//递归函数
public static void GetFiles(string path,ArrayList files)
{
//获取当前目录下的文件
foreach(string file in Directory.GetFiles(path))
{
files.Add(file);
}
//获取当前目录下的子目录文件
foreach(string dir in Directory.GetDirectories(path))
{
GetFiles(dir,files);
}
}
}
}
====================================
//C#遍历文件夹
using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.IO;
using System.Net;
using System.Text;
using System.Diagnostics;
namespace ZZConsoleTest
{
public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string [] fileNameArray = GetFileNamesWithArray("D://zhzuo//music");
foreach(string name in fileNameArray)
{
Console.WriteLine(name);
}
Console.WriteLine("================================================");
ArrayList fileNames = GetFileNamesWithArrayList("D://zhzuo//music");
foreach(object name in fileNameArray)
{
Console.WriteLine((string)name);
}
Console.WriteLine("完成");
Console.ReadLine();
}
//递归方法
private static void ListAllFiles(string sourceDir,ArrayList al)
{
foreach(string fileName in Directory.GetFiles(sourceDir))
{
al.Add(fileName);
}
foreach(string subDir in Directory.GetDirectories(sourceDir))
{
ListAllFiles(subDir,al);
}
}
//用ArrayList方式返回
public static ArrayList GetFileNamesWithArrayList(string sourceDir)
{
ArrayList al = new ArrayList();
ListAllFiles(sourceDir,al);
return al;
}
//用string[]方式返回
public static string[] GetFileNamesWithArray(string sourceDir)
{
ArrayList al = new ArrayList();
ListAllFiles(sourceDir,al);
return (string[])al.ToArray(typeof(string));
}
}
}
==================================
/// <summary>
/// 将指定目录下的子目录和文件生成xml文档
/// </summary>
/// <param name="targetDir">根目录</param>
/// <returns>返回XmlDocument对象</returns>
public static XmlDocument CreateXml(string targetDir)
{
XmlDocument myDocument = new XmlDocument();
XmlDeclaration declaration = myDocument.CreateXmlDeclaration("1.0","utf-8",null);
myDocument.AppendChild(declaration);
XmlElement rootElement = myDocument.CreateElement(targetDir.Substring(targetDir.LastIndexOf("//")+1));
myDocument.AppendChild(rootElement);
foreach(string fileName in Directory.GetFiles(targetDir))
{
XmlElement childElement = myDocument.CreateElement("File");
childElement.InnerText = fileName.Substring(fileName.LastIndexOf("//")+1);
rootElement.AppendChild(childElement);
}
foreach(string directory in Directory.GetDirectories(targetDir))
{
XmlElement childElement = myDocument.CreateElement("Directory");
childElement.SetAttribute("Name",directory.Substring(directory.LastIndexOf("//")+1));
rootElement.AppendChild(childElement);
CreateBranch(directory,childElement,myDocument);
}
return myDocument;
}
/// <summary>
/// 生成Xml分支
/// </summary>
/// <param name="targetDir">子目录</param>
/// <param name="xmlNode">父目录XmlDocument</param>
/// <param name="myDocument">XmlDocument对象</param>
private static void CreateBranch(string targetDir,XmlElement xmlNode,XmlDocument myDocument)
{
foreach(string fileName in Directory.GetFiles(targetDir))
{
XmlElement childElement = myDocument.CreateElement("File");
childElement.InnerText = fileName.Substring(fileName.LastIndexOf("//")+1);
xmlNode.AppendChild(childElement);
}
foreach(string directory in Directory.GetDirectories(targetDir))
{
XmlElement childElement = myDocument.CreateElement("Directory");
childElement.SetAttribute("Name",directory.Substring(directory.LastIndexOf("//")+1));
xmlNode.AppendChild(childElement);
CreateBranch(directory,childElement,myDocument);
}
}