var mediaListFile = MakeFilePath("index.wpl", di.FullName);
XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));
xDoc.Save(mediaListFile);
Using recursion, I created a collection of media files, which I wrote out to an XML file on my local drive. After creating the XML file, I was able to double-click on the file and Windows Media Player would begin. The XML file was a Windows Media Play List.
The code is below:
private TreeNode CreateDirectoryNode(DirectoryInfo di)
{
var directoryNode = new TreeNode(di.Name);
foreach (var directory in di.GetDirectories())
{
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
}
var files = GetFiles(di);
foreach (var file in files)
{
directoryNode.Nodes.Add(new TreeNode(file));
}
if (files.Count() > 0)
{
var mediaListFile = MakeFilePath("index.wpl", di.FullName);
XDocument xDoc = new XDocument(new XElement("smil", new XElement("head", new XElement("title", di.Name)), new XElement("body", new XElement("seq", files.Select(f => new XElement("media", new XAttribute("src", f)))))));
xDoc.Save(mediaListFile);
}
return directoryNode;
}
private bool FilterFiles(string file)
{
Regex FilterFile = new Regex(@"^[\w\-. ]+(\.mp4|\.wmv|\.mov|\.avi|\.mp3)$");
return FilterFile.IsMatch(file);
}
private string StripFile(string oldFile, string path)
{
Regex SpecialChars = new Regex(@"[^a-zA-Z0-9._ ]+");
if (SpecialChars.IsMatch(oldFile))
{
var newFile = AddFilePrefix(SpecialChars.Replace(oldFile, "_"));
var newFilePath = MakeFilePath(newFile, path);
if (!File.Exists(newFilePath))
{
var oldFilePath = MakeFilePath(oldFile, path);
File.Copy(oldFilePath, newFilePath);
}
oldFile = newFile;
}
return oldFile;
}
private List<string> GetFiles(DirectoryInfo di)
{
List<string> files = new List<string>();
string stripedFile;
string path = di.FullName;
foreach (var file in di.GetFiles())
{
if (FilterFiles(file.Name))
{
stripedFile = StripFile(file.Name, path);
files.Add(stripedFile);
}
}
return files;
}
“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”
Henry Ford