-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
137 lines (104 loc) · 6.09 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static string xmlfilename = "catelog.xml"; //define of filename
static bool xmlfileexist = false;
static void Main(string[] args)
{
Program p = new Program();
//string path = Console.ReadLine();
string path = "D:\\wy\\video_clip";
p.createXMLFile(path);
Console.Read();
}
public XmlDocument xmldoc;
public XmlNode xmlnode;
public XmlElement xmlelem;
public XmlDeclaration xmldecl;
private void createXMLFile(string path)
{
string[] aformatList = {"avi", "mp4", "mpeg", "mpg", "divx", "mov", "wmv"};
try
{
//////////////判断xml文件是否存在,如果不存在则创建新的xml文件
if (System.IO.File.Exists(xmlfilename)) { //如果a.xml已经存在,则载入
xmldoc.Load(xmlfilename);
xmlfileexist = true;
}
else
{ //如果a.xml不存在,则创建新的xml文件。
xmldoc = new XmlDocument();
xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmldoc.AppendChild(xmldecl); //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
xmlelem = xmldoc.CreateElement("", "catalog", ""); //加入一个根元素
xmldoc.AppendChild(xmlelem);
}
///////////////
XmlNode root = xmldoc.SelectSingleNode("catalog"); //查找<Catelog>
///////////////遍历文件夹和文件,对应每个视频文件建立相应的vnode结点
string[] dir = Directory.GetDirectories(path); //当前文件夹列表
DirectoryInfo fdir = new DirectoryInfo(path);
FileInfo[] file = fdir.GetFiles();
//FileInfo[] file = Directory.GetFiles(path); //文件列表
XmlElement xeTemp, xeTempSub, xeTempSub1;
if (file.Length != 0 || dir.Length != 0) //当前目录文件或文件夹不为空
{
foreach (FileInfo f in file) //显示当前目录所有文件,各种格式的都有,没有文件夹
{
string sfullName = f.FullName.ToString(); //取得文件的完整路径字符串
int iLastIndexOfSlash = sfullName.LastIndexOf('\\');
int iLastIndexOfDot = sfullName.LastIndexOf('.');
string sformat = sfullName.Substring(iLastIndexOfDot + 1); //取出了文件类型
string sname = sfullName.Substring(iLastIndexOfSlash + 1, iLastIndexOfDot - iLastIndexOfSlash - 1); //取出了文件名
string sformatLow = sformat.ToLower();
long sLength = f.Length;
foreach (string format in aformatList) //
{
if (sformatLow.CompareTo(format) == 0){ //如果找到了视频格式文件,则进行结点的创建
xeTemp = xmldoc.CreateElement("", "vnode", ""); //创建视频结点
xeTemp.SetAttribute("name", sname);
xeTemp.SetAttribute("format", sformatLow);
xeTempSub = xmldoc.CreateElement("", "vsize", "");
xeTempSub.InnerText = sLength.ToString();
xeTemp.AppendChild(xeTempSub);
xeTempSub = xmldoc.CreateElement("", "vdir", "");
xeTempSub.InnerText = sfullName;
xeTemp.AppendChild(xeTempSub);
xeTempSub = xmldoc.CreateElement("", "vdataset", "");
xeTempSub.InnerText = "vdataset"; //TODO
xeTemp.AppendChild(xeTempSub);
xeTempSub = xmldoc.CreateElement("", "features", "");
xeTempSub.SetAttribute("fNumber", "0"); //default:0
xeTempSub1 = xmldoc.CreateElement("", "");
xeTemp.AppendChild(xeTempSub);
root.AppendChild(xeTemp);
}
}
Console.Write(sfullName + "\n sname: " + sname + "\n sformat: " + sformat); break;
//Console.Write(" -- " + filename + "\n");
}
foreach (string d in dir)
{
//Console.Write("d: " + d + "\n");
int startIndex = d.LastIndexOf("\\");
string fileName = d.Substring(startIndex + 1); //文件夹名称
//Console.Write("" + fileName + "\n");
createXMLFile(d); //递归
}
}
else
return;
xmldoc.Save("a.xml"); //保存a.xml文件
}
catch { };
}
}
}