using System; using System.Diagnostics; using System.IO; namespace m3uTool { /// /// Summary description for m3uFromDirectory. /// public class M3uCreate { /// /// Use the parent's directory name for the m3u name /// private bool mUseGeneratedOuputFilename = true; /// /// Directory to grab the files from /// private DirectoryInfo mInputDir; /// /// Ouput m3u filename, if it isn't generated /// private string mOutputFilename = null; /// /// Filter for input filenames /// private string mFileFilter = "*.mp3"; public bool UseGeneratedOuputFilename { get { return mUseGeneratedOuputFilename; } set { mUseGeneratedOuputFilename = value; } } public string OutputFilename { get { if (mUseGeneratedOuputFilename) { Debug.WriteLine("OutputFilename:" + mInputDir.FullName + "\\" + mInputDir.Name + ".m3u"); return mInputDir.FullName + "\\" + mInputDir.Name + ".m3u"; } return mOutputFilename; } set { mOutputFilename = value; mUseGeneratedOuputFilename = false; } } public M3uCreate(string dir) : this (new DirectoryInfo(dir)) { } public M3uCreate(DirectoryInfo inputDir) { mInputDir = inputDir; } /// /// /// /// /// dir: "\foo\bar\" file: "\foo\bar\myfile" output: "" /// dir: "\foo\" file: "\foo\bar\myfile" output: ".." /// dir: "\" file: "\foo\bar\myfile" output: "..\.." /// dir: "\har" file: "\foo\bar\myfile" output: "..\..\har" /// dir: "\foo\bar\hey\" file: "\foo\bar\myfile" output: "hey\" /// /// /// /// private string GetPath(DirectoryInfo dir, FileInfo file) { string source = dir.FullName; string target = file.Directory.FullName; return GetPath(source, target); } /// /// Gets the path. /// /// The source. /// The target. /// private string GetPath(string source, string target) { int lastCommonIndex = GetLastCommonIndex(source, target); Debug.WriteLine(String.Format("s:{0} , t:{1} lastCommon:{2} ", source, target, lastCommonIndex)); int slashCount = CountCharacters(source, lastCommonIndex + 1, '\\'); string path = ""; for (int i = 0; i < slashCount; i++) { path += "..\\"; } path += target.Substring(lastCommonIndex); if (path.Length > 0 && path[path.Length - 1] != '\\') path += "\\"; Debug.WriteLine(path); return path; } private int CountCharacters(string str, int startIdx, char c) { int charsCount = 0; int currIdx = startIdx; while (currIdx < str.Length && (currIdx = str.IndexOf(c, currIdx) + 1) != 0) charsCount++; return charsCount; } private int GetLastCommonIndex(string str1, string str2) { int i = 0; while (i < str1.Length && i < str2.Length && Char.ToLower(str1[i]) == Char.ToLower(str2[i])) i++; return i; } public void Create() { string fileList = ""; DirectoryInfo ouputDirectory = (new FileInfo(OutputFilename)).Directory; foreach (FileInfo info in mInputDir.GetFiles(mFileFilter)) { string fileName = GetPath(ouputDirectory, info) + info.Name; Debug.WriteLine(fileName); fileList += fileName + Environment.NewLine; } SaveToFile(fileList, OutputFilename); } private void SaveToFile(string contents, string outputFilename) { using (TextWriter tw = new StreamWriter(outputFilename)) { tw.Write(contents); } } } }