1 using System.Collections.Generic;
2 using System.ComponentModel;
5 using System.Xml.Serialization;
7 namespace DirectOutput.General
12 public class FilePattern : INotifyPropertyChanged, IXmlSerializable
16 #region IXmlSerializable Member
21 public System.Xml.Schema.XmlSchema GetSchema()
30 public void ReadXml(System.Xml.XmlReader reader)
32 Pattern = reader.ReadString();
33 if (!reader.IsEmptyElement)
35 reader.ReadEndElement();
39 reader.ReadStartElement();
47 public void WriteXml(System.Xml.XmlWriter writer)
49 writer.WriteString(Pattern);
54 #region INotifyPropertyChanged Member
61 private void NotifyPropertyChanged(
string PropertyName)
63 if (PropertyChanged != null)
65 PropertyChanged(
this,
new PropertyChangedEventArgs(PropertyName));
71 private string _Pattern=
"";
81 get {
return _Pattern; }
84 if (_Pattern != value)
87 NotifyPropertyChanged(
"Pattern");
97 public string ReplacePlaceholders(Dictionary<string, string> ReplaceValues = null)
100 if (ReplaceValues != null)
102 foreach (KeyValuePair<string, string> KV
in ReplaceValues)
104 P = P.Replace(
"{" + (KV.Key) +
"}", KV.Value);
116 public List<FileInfo> GetMatchingFiles(Dictionary<string, string> ReplaceValues = null)
118 if (Pattern.IsNullOrWhiteSpace())
return new List<FileInfo>();
120 string P = ReplacePlaceholders(ReplaceValues);
126 DirectoryInfo FilesDirectory =
new DirectoryInfo((Path.GetDirectoryName(P).IsNullOrWhiteSpace() ?
"." : Path.GetDirectoryName(P)));
128 if (FilesDirectory.Exists)
130 return FilesDirectory.GetFiles(Path.GetFileName(P)).ToList<FileInfo>();
134 return new List<FileInfo>();
139 return new List<FileInfo>();
148 public FileInfo GetFirstMatchingFile(Dictionary<string, string> ReplaceValues = null)
150 List<FileInfo> L = GetMatchingFiles(ReplaceValues);
172 if (Pattern.IsNullOrWhiteSpace())
return true;
176 FileInfo DummyFileInfo =
new FileInfo(Pattern.Replace(
"*",
"test").Replace(
"?",
"X"));
183 bool BracketOpen =
false;
184 bool JustOpend =
false;
185 foreach (
char C
in Pattern)
190 if (BracketOpen)
return false;
195 if (!BracketOpen)
return false;
196 if (JustOpend)
return false;
203 if (BracketOpen)
return false;
211 if (BracketOpen)
return false;
225 this.Pattern = Pattern;
236 public override string ToString()