这是一个遍历指定目录下所有的目录并写成XML文件,应该对你有帮助
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;
namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
const  string mypath=@"D:\nba2004";
XmlDocument doc=new XmlDocument(); public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(192, 232);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click1);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
}


private void button1_Click1(object sender, System.EventArgs e)
{

string MyPath=mypath.ToUpper();
doc.LoadXml("<FileInfo Name='"+MyPath+"'></FileInfo>");
GetFileInfo(MyPath,doc.DocumentElement);
doc.Save(@"d:\fileinfo.xml");
}
private void GetFileInfo(string strpath,System.Xml.XmlNode node)
{
DirectoryInfo DirInfo=new DirectoryInfo(strpath);
foreach(DirectoryInfo dirinfo in DirInfo.GetDirectories())
{
XmlElement elem = doc.CreateElement("dir");
elem.SetAttribute("Name",dirinfo.Name);
elem.SetAttribute("LastWriteTime",dirinfo.LastWriteTime.ToString());
node.AppendChild(elem); 
GetFileInfo(dirinfo.FullName,elem);
}

foreach(FileInfo fileinfo in DirInfo.GetFiles())
{
XmlElement elem = doc.CreateElement("file");
elem.SetAttribute("Name",fileinfo.Name);
elem.SetAttribute("LastWriteTime",fileinfo.LastWriteTime.ToString());
elem.SetAttribute("Length",fileinfo.Length.ToString());
node.AppendChild(elem);
}
} }

}

解决方案 »

  1.   

    System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
      

  2.   

    你是说
    C:\Documents and Settings\Administrator\My Documents  ??
      

  3.   

    Environment.SpecialFolder 枚举指定用于检索系统特殊文件夹的目录路径的枚举常数。
    系统特殊文件夹是包含公共信息的文件夹,如“Program Files”、“Programs”、“System”或“Startup”。特殊文件夹在默认情况下由系统设置,或者由用户在安装 Windows 的某个版本时显式进行设置。GetFolderPath 方法使用这些枚举常数来指定要检索的特殊文件夹路径。有关特殊文件夹的更多信息,请参见 http://msdn.microsoft.com/library/en-us/shellcc/platform/Shell/reference/enums/csidl.asp 中的 CSIDL Values 主题。成员
    成员名称 说明 
    ApplicationData 目录,它用作当前漫游用户的应用程序特定数据的公共储存库。 
    漫游用户在网络上的多台计算机上工作。漫游用户的配置文件保存在网络服务器上,当用户登录到某个系统上时,它会加载到该系统。
     
    CommonApplicationData 目录,它用作所有用户使用的应用程序特定数据的公共储存库。 
    CommonProgramFiles 用于应用程序间共享的组件的目录。 
    Cookies 用作 Internet Cookie 的公共储存库的目录。 
    Desktop 逻辑桌面,而不是物理文件系统位置。 
    DesktopDirectory 用于物理上存储桌面上的文件对象的目录。 
    不应将此目录与桌面文件夹本身混淆,后者是虚拟文件夹。
     
    Favorites 用作用户收藏夹项的公共储存库的目录。 
    History 用作 Internet 历史记录项的公共储存库的目录。 
    InternetCache 用作 Internet 临时文件的公共储存库的目录。 
    LocalApplicationData 目录,它用作当前非漫游用户使用的应用程序特定数据的公共储存库。 
    MyComputer “我的电脑”文件夹。 
    MyMusic “My Music”文件夹。 
    MyPictures “My Pictures”文件夹。 
    Personal 用作文档的公共储存库的目录。 
    ProgramFiles “Program files”目录。 
    Programs 包含用户程序组的目录。 
    Recent 包含用户最近使用过的文档的目录。 
    SendTo 包含“发送”菜单项的目录。 
    StartMenu 包含“开始”菜单项的目录。 
    Startup 对应于用户的“启动”程序组的目录。 
    每当用户登录、启动 Windows NT 或更高版本或启动 Windows 98 时,系统均会启动这些程序。
     
    System “System”目录。 
    Templates 用作文档模板的公共储存库的目录。 
      

  4.   

    string str=System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);    
    System.IO.DirectoryInfo mDir=new System.IO.DirectoryInfo(str);
    MessageBox.Show(mDir.Parent.FullName);  
    --------------------------
    这个行但不太理想。