FolderBrowserDialog fold = new FolderBrowserDialog();
if (fold.ShowDialog() == DialogResult.OK)
{
if (fold.SelectedPath.Trim() != "")
{
FoldPath = fold.SelectedPath.Trim();
}
}
list.Clear();
this.listView1.Items.Clear();
ReadFile(FoldPath, "");
private void ReadFile(string FoldPath, string FileName)
{
string[] filenames = Directory.GetFileSystemEntries(FoldPath);
foreach (string file in filenames)// 遍历所有的文件和目录
{
string folderName = file.Substring(file.LastIndexOf("\\") + 1);
string desfolderdir = FileName + "\\" + folderName;
if (FoldPath.LastIndexOf("\\") == (FoldPath.Length - 1))
{
desfolderdir = FoldPath + folderName;
}
ListViewItem lvi = new ListViewItem();
lvi.Text = "1";
lvi.Tag = file;
if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string currentdir = file.Substring(file.LastIndexOf("\\") + 1);
ReadFile(file, desfolderdir);
}
else
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
if (desfolderdir.Substring(0, 1) == "\\")
{
desfolderdir = desfolderdir.Substring(1, desfolderdir.Length - 1);
}
lvi.SubItems.Add(desfolderdir);
this.listView1.Items.Add(lvi);
list.Add(desfolderdir);
}
}
}
try
{
if (File.Exists(FoldPath + "\\" + xmlName))
{
File.Delete(FoldPath + "\\" + xmlName);
}
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null);
doc.AppendChild(dec);
XmlElement root = doc.CreateElement("resources");
root.SetAttribute("version", this.txtVis.Text.Trim());
doc.AppendChild(root);
foreach (string item in list)
{
XmlNode node = doc.CreateElement("item");
node.InnerText = item;
root.AppendChild(node);
}
doc.Save(FoldPath + "\\" + xmlName);
MessageBox.Show("创建成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}