DirectorySelector.xaml.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. namespace ConfigFileManager.Controls.FileSelector;
  2. public partial class DirectorySelector : UserControl
  3. {
  4. public DirectorySelector()
  5. {
  6. InitializeComponent();
  7. }
  8. public string Extension
  9. {
  10. get { return (string)GetValue(ExtensionProperty); }
  11. set { SetValue(ExtensionProperty, value); }
  12. }
  13. public static readonly DependencyProperty ExtensionProperty =
  14. DependencyProperty.Register("Extension", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default));
  15. public ObservableCollection<FileDisplay> Displays
  16. {
  17. get { return (ObservableCollection<FileDisplay>)GetValue(DisplaysProperty); }
  18. set { SetValue(DisplaysProperty, value); }
  19. }
  20. public static readonly DependencyProperty DisplaysProperty =
  21. DependencyProperty.Register("Displays", typeof(ObservableCollection<FileDisplay>), typeof(DirectorySelector), new PropertyMetadata(default));
  22. public ObservableDictionary<int, FileDisplay> Parents
  23. {
  24. get { return (ObservableDictionary<int, FileDisplay>)GetValue(ParentsProperty); }
  25. set { SetValue(ParentsProperty, value); }
  26. }
  27. public static readonly DependencyProperty ParentsProperty =
  28. DependencyProperty.Register("Parents", typeof(ObservableDictionary<int, FileDisplay>), typeof(DirectorySelector), new PropertyMetadata(default));
  29. public string Title
  30. {
  31. get { return (string)GetValue(TitleProperty); }
  32. set { SetValue(TitleProperty, value); }
  33. }
  34. public static readonly DependencyProperty TitleProperty =
  35. DependencyProperty.Register("Title", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default));
  36. public string InitialPath
  37. {
  38. get { return (string)GetValue(InitialPathProperty); }
  39. set { SetValue(InitialPathProperty, value); }
  40. }
  41. public static readonly DependencyProperty InitialPathProperty =
  42. DependencyProperty.Register("InitialPath", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default, PropertyChangedCallback));
  43. private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  44. {
  45. if (d is not DirectorySelector selector)
  46. return;
  47. if (e.NewValue is not string path)
  48. return;
  49. selector.Displays ??= [];
  50. selector.Displays.Clear();
  51. selector.Parents ??= [];
  52. selector.Parents.Clear();
  53. DirectoryInfo? temp = new(path);
  54. selector.SetCurrent(temp);
  55. }
  56. private string _Current = "";
  57. private static void GetFullPath(DirectoryInfo dir, ref List<FileDisplay> paths)
  58. {
  59. paths ??= [];
  60. FileDisplay display = new()
  61. {
  62. Name = dir.Name,
  63. Path = dir.FullName,
  64. ImageSource = Application.Current.Resources["Icon_Folder"],
  65. Type = DisType.Folder
  66. };
  67. paths.Add(display);
  68. if (dir.Parent is not null)
  69. GetFullPath(dir.Parent, ref paths);
  70. }
  71. private void Button_Click(object sender, RoutedEventArgs e)
  72. {
  73. var t = (Button)e.Source;
  74. FileDisplay d = (FileDisplay)t.CommandParameter;
  75. if (string.IsNullOrEmpty(d.Path))
  76. return;
  77. if (d.Type == DisType.File)
  78. {
  79. try
  80. {
  81. System.Diagnostics.Process.Start("notepad.exe", d.Path);
  82. }
  83. catch
  84. {
  85. }
  86. return;
  87. }
  88. DirectoryInfo dir = new(d.Path);
  89. List<FileDisplay> s = [];
  90. GetFullPath(dir, ref s);
  91. Parents ??= [];
  92. Parents.Clear();
  93. for (int i = s.Count - 1; i >= 0; i--)
  94. {
  95. Parents[i] = s[i];
  96. }
  97. Displays ??= [];
  98. Displays.Clear();
  99. _Current = d.Path;
  100. FileInfo[] files = dir.GetFiles();
  101. DirectoryInfo[] dics = dir.GetDirectories();
  102. foreach (DirectoryInfo item in dics)
  103. {
  104. if (item.Attributes.HasFlag(FileAttributes.Hidden))
  105. continue;
  106. if (item.Name.StartsWith('.'))
  107. continue;
  108. FileDisplay display = new()
  109. {
  110. Name = item.Name,
  111. Path = item.FullName,
  112. ImageSource = Application.Current.Resources["Icon_Folder"],
  113. Type = DisType.Folder
  114. };
  115. Displays.Add(display);
  116. }
  117. foreach (var item in files)
  118. {
  119. if (item.Attributes.HasFlag(FileAttributes.Hidden))
  120. continue;
  121. if (this.Extension is not null && item.Extension != this.Extension)
  122. continue;
  123. FileDisplay display = new()
  124. {
  125. Name = item.Name.Remove(item.Name.LastIndexOf(item.Extension), item.Extension.Length),
  126. Path = item.FullName,
  127. ImageSource = Application.Current.Resources["Icon_File"],
  128. FileType = item.Extension,
  129. Type = DisType.File
  130. };
  131. Displays.Add(display);
  132. }
  133. }
  134. private void Button_Click_1(object sender, RoutedEventArgs e)
  135. {
  136. if (string.IsNullOrEmpty(_Current))
  137. return;
  138. if (_Current == InitialPath)
  139. {
  140. return;
  141. }
  142. Displays ??= [];
  143. Displays.Clear();
  144. Parents ??= [];
  145. Parents.Clear();
  146. DirectoryInfo? temp = new DirectoryInfo(_Current).Parent;
  147. this.SetCurrent(temp);
  148. }
  149. private void SetCurrent(DirectoryInfo? directoryInfo)
  150. {
  151. DirectoryInfo? temp = directoryInfo;
  152. if (temp is null)
  153. {
  154. DriveInfo[] drives = DriveInfo.GetDrives();
  155. foreach (DriveInfo basePath in drives)
  156. {
  157. FileDisplay display = new()
  158. {
  159. Name = basePath.Name,
  160. Path = basePath.Name,
  161. ImageSource = Application.Current.Resources["Icon_Folder"],
  162. Type = DisType.Folder
  163. };
  164. Displays.Add(display);
  165. }
  166. return;
  167. }
  168. List<FileDisplay> s = [];
  169. GetFullPath(temp, ref s);
  170. for (int i = s.Count - 1; i >= 0; i--)
  171. {
  172. Parents[i] = s[i];
  173. }
  174. _Current = temp.FullName;
  175. FileInfo[] files = new DirectoryInfo(_Current).GetFiles();
  176. DirectoryInfo[] dics = new DirectoryInfo(_Current).GetDirectories();
  177. foreach (DirectoryInfo item in dics)
  178. {
  179. if (item.Attributes.HasFlag(FileAttributes.Hidden))
  180. continue;
  181. if (item.Name.StartsWith('.'))
  182. continue;
  183. if (!string.IsNullOrEmpty(this.Extension) && this.Extension != item.Extension)
  184. continue;
  185. FileDisplay display = new()
  186. {
  187. Name = item.Name,
  188. Path = item.FullName,
  189. ImageSource = Application.Current.Resources["Icon_Folder"],
  190. Type = DisType.Folder
  191. };
  192. Displays.Add(display);
  193. }
  194. foreach (var item in files)
  195. {
  196. if (item.Attributes.HasFlag(FileAttributes.Hidden))
  197. continue;
  198. if (!string.IsNullOrEmpty(this.Extension) && this.Extension != item.Extension)
  199. continue;
  200. FileDisplay display = new()
  201. {
  202. Name = item.Name.Replace(item.Extension, ""),
  203. Path = item.FullName,
  204. ImageSource = Application.Current.Resources["Icon_File"],
  205. FileType = item.Extension,
  206. Type = DisType.File
  207. };
  208. Displays.Add(display);
  209. }
  210. }
  211. }
  212. public partial class FileDisplay : ObservableObject
  213. {
  214. [ObservableProperty]
  215. private string? _Name;
  216. [ObservableProperty]
  217. private string? _Path;
  218. [ObservableProperty]
  219. private object? _ImageSource;
  220. [ObservableProperty]
  221. private object? _FileType;
  222. [ObservableProperty]
  223. private DisType _Type;
  224. }
  225. public enum DisType
  226. {
  227. File,
  228. Folder
  229. }