123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- namespace ConfigFileManager.Controls.FileSelector;
- public partial class DirectorySelector : UserControl
- {
- public DirectorySelector()
- {
- InitializeComponent();
- }
- public string Extension
- {
- get { return (string)GetValue(ExtensionProperty); }
- set { SetValue(ExtensionProperty, value); }
- }
- public static readonly DependencyProperty ExtensionProperty =
- DependencyProperty.Register("Extension", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default));
- public ObservableCollection<FileDisplay> Displays
- {
- get { return (ObservableCollection<FileDisplay>)GetValue(DisplaysProperty); }
- set { SetValue(DisplaysProperty, value); }
- }
- public static readonly DependencyProperty DisplaysProperty =
- DependencyProperty.Register("Displays", typeof(ObservableCollection<FileDisplay>), typeof(DirectorySelector), new PropertyMetadata(default));
- public ObservableDictionary<int, FileDisplay> Parents
- {
- get { return (ObservableDictionary<int, FileDisplay>)GetValue(ParentsProperty); }
- set { SetValue(ParentsProperty, value); }
- }
- public static readonly DependencyProperty ParentsProperty =
- DependencyProperty.Register("Parents", typeof(ObservableDictionary<int, FileDisplay>), typeof(DirectorySelector), new PropertyMetadata(default));
- public string Title
- {
- get { return (string)GetValue(TitleProperty); }
- set { SetValue(TitleProperty, value); }
- }
- public static readonly DependencyProperty TitleProperty =
- DependencyProperty.Register("Title", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default));
- public string InitialPath
- {
- get { return (string)GetValue(InitialPathProperty); }
- set { SetValue(InitialPathProperty, value); }
- }
- public static readonly DependencyProperty InitialPathProperty =
- DependencyProperty.Register("InitialPath", typeof(string), typeof(DirectorySelector), new PropertyMetadata(default, PropertyChangedCallback));
- private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (d is not DirectorySelector selector)
- return;
- if (e.NewValue is not string path)
- return;
- selector.Displays ??= [];
- selector.Displays.Clear();
- selector.Parents ??= [];
- selector.Parents.Clear();
- DirectoryInfo? temp = new(path);
- selector.SetCurrent(temp);
- }
- private string _Current = "";
- private static void GetFullPath(DirectoryInfo dir, ref List<FileDisplay> paths)
- {
- paths ??= [];
- FileDisplay display = new()
- {
- Name = dir.Name,
- Path = dir.FullName,
- ImageSource = Application.Current.Resources["Icon_Folder"],
- Type = DisType.Folder
- };
- paths.Add(display);
- if (dir.Parent is not null)
- GetFullPath(dir.Parent, ref paths);
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- var t = (Button)e.Source;
- FileDisplay d = (FileDisplay)t.CommandParameter;
- if (string.IsNullOrEmpty(d.Path))
- return;
- if (d.Type == DisType.File)
- {
- try
- {
- System.Diagnostics.Process.Start("notepad.exe", d.Path);
- }
- catch
- {
- }
- return;
- }
- DirectoryInfo dir = new(d.Path);
- List<FileDisplay> s = [];
- GetFullPath(dir, ref s);
- Parents ??= [];
- Parents.Clear();
- for (int i = s.Count - 1; i >= 0; i--)
- {
- Parents[i] = s[i];
- }
- Displays ??= [];
- Displays.Clear();
- _Current = d.Path;
- FileInfo[] files = dir.GetFiles();
- DirectoryInfo[] dics = dir.GetDirectories();
- foreach (DirectoryInfo item in dics)
- {
- if (item.Attributes.HasFlag(FileAttributes.Hidden))
- continue;
- if (item.Name.StartsWith('.'))
- continue;
- FileDisplay display = new()
- {
- Name = item.Name,
- Path = item.FullName,
- ImageSource = Application.Current.Resources["Icon_Folder"],
- Type = DisType.Folder
- };
- Displays.Add(display);
- }
- foreach (var item in files)
- {
- if (item.Attributes.HasFlag(FileAttributes.Hidden))
- continue;
- if (this.Extension is not null && item.Extension != this.Extension)
- continue;
- FileDisplay display = new()
- {
- Name = item.Name.Remove(item.Name.LastIndexOf(item.Extension), item.Extension.Length),
- Path = item.FullName,
- ImageSource = Application.Current.Resources["Icon_File"],
- FileType = item.Extension,
- Type = DisType.File
- };
- Displays.Add(display);
- }
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(_Current))
- return;
- if (_Current == InitialPath)
- {
- return;
- }
- Displays ??= [];
- Displays.Clear();
- Parents ??= [];
- Parents.Clear();
- DirectoryInfo? temp = new DirectoryInfo(_Current).Parent;
- this.SetCurrent(temp);
- }
- private void SetCurrent(DirectoryInfo? directoryInfo)
- {
- DirectoryInfo? temp = directoryInfo;
- if (temp is null)
- {
- DriveInfo[] drives = DriveInfo.GetDrives();
- foreach (DriveInfo basePath in drives)
- {
- FileDisplay display = new()
- {
- Name = basePath.Name,
- Path = basePath.Name,
- ImageSource = Application.Current.Resources["Icon_Folder"],
- Type = DisType.Folder
- };
- Displays.Add(display);
- }
- return;
- }
- List<FileDisplay> s = [];
- GetFullPath(temp, ref s);
- for (int i = s.Count - 1; i >= 0; i--)
- {
- Parents[i] = s[i];
- }
- _Current = temp.FullName;
- FileInfo[] files = new DirectoryInfo(_Current).GetFiles();
- DirectoryInfo[] dics = new DirectoryInfo(_Current).GetDirectories();
- foreach (DirectoryInfo item in dics)
- {
- if (item.Attributes.HasFlag(FileAttributes.Hidden))
- continue;
- if (item.Name.StartsWith('.'))
- continue;
- if (!string.IsNullOrEmpty(this.Extension) && this.Extension != item.Extension)
- continue;
- FileDisplay display = new()
- {
- Name = item.Name,
- Path = item.FullName,
- ImageSource = Application.Current.Resources["Icon_Folder"],
- Type = DisType.Folder
- };
- Displays.Add(display);
- }
- foreach (var item in files)
- {
- if (item.Attributes.HasFlag(FileAttributes.Hidden))
- continue;
- if (!string.IsNullOrEmpty(this.Extension) && this.Extension != item.Extension)
- continue;
- FileDisplay display = new()
- {
- Name = item.Name.Replace(item.Extension, ""),
- Path = item.FullName,
- ImageSource = Application.Current.Resources["Icon_File"],
- FileType = item.Extension,
- Type = DisType.File
- };
- Displays.Add(display);
- }
- }
- }
- public partial class FileDisplay : ObservableObject
- {
- [ObservableProperty]
- private string? _Name;
- [ObservableProperty]
- private string? _Path;
- [ObservableProperty]
- private object? _ImageSource;
- [ObservableProperty]
- private object? _FileType;
- [ObservableProperty]
- private DisType _Type;
- }
- public enum DisType
- {
- File,
- Folder
- }
|