-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
169 lines (134 loc) · 4.63 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Ookii.Dialogs.Wpf;
namespace ImageViewerFolder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string sFolderPath;
private bool bSearchSubs;
private bool bFolderInit;
private static readonly string ImageExtension = ".JPG";
private static readonly string MovieExtension = ".MOV";
private List<string> intersection = new();
public MainWindow()
{
InitializeComponent();
bSearchSubs = false;
bFolderInit = false;
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
VistaFolderBrowserDialog folderDialog = new VistaFolderBrowserDialog();
bool? result = folderDialog.ShowDialog();
if (result.HasValue && result.Value)
{
sFolderPath = folderDialog.SelectedPath;
FolderDir.Content = sFolderPath;
bFolderInit = true;
UpdateList();
}
}
private void RootOnly_Click(object sender, RoutedEventArgs e)
{
bSearchSubs = false;
if (bFolderInit)
{
UpdateList();
}
}
private void RootSub_Click(object sender, RoutedEventArgs e)
{
bSearchSubs = true;
if (bFolderInit)
{
UpdateList();
}
}
private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
int count = 0;
foreach (string f in intersection) //todo: use delegates
{
try
{
System.IO.File.Delete(f + MovieExtension);
count++;
}
catch (Exception ex)
{
count--;
string err = "Program has encountered an error while deleting file: " + f + MovieExtension + "\n" + ex.Message.ToString();
_ = MessageBox.Show(err);
}
}
UpdateList();
string info = "Removed: " + count.ToString() + " movies.";
_ = MessageBox.Show(info);
}
private void DirBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ImgBox.Source = null;
if (DirBox.Items.Count > 0)
{
string imgPath = e.AddedItems[0].ToString();
BitmapImage bitmap = new();
bitmap.BeginInit();
bitmap.UriSource = new Uri(imgPath);
bitmap.EndInit();
ImgBox.Source = bitmap;
}
}
private void UpdateList()
{
DirBox.Items.Clear();
List<string> vImgNames = new List<string>();
List<string> vMovNames = new List<string>();
List<string> vAllNames = new List<string>();
try
{
if (!bSearchSubs)
{
vAllNames = Directory.GetFiles(sFolderPath).ToList();
}
if (bSearchSubs)
{
vAllNames = Directory.GetFiles(sFolderPath, "*", SearchOption.AllDirectories).ToList();
}
foreach (string f in vAllNames.ToList())
{
if (ImageExtension.Contains(System.IO.Path.GetExtension(f).ToUpperInvariant()))
{
vImgNames.Add(f.Split('.')[0]);
}
else if (MovieExtension.Contains(System.IO.Path.GetExtension(f).ToUpperInvariant()))
{
vMovNames.Add(f.Split('.')[0]);
}
}
intersection = vImgNames.Select(i => i.ToString()).Intersect(vMovNames).ToList();
foreach (var f in intersection)
{
_ = DirBox.Items.Add(f + ".JPG");
}
if (DirBox.Items.Count > 0)
RemoveButton.Visibility = Visibility.Visible;
else
RemoveButton.Visibility = Visibility.Hidden;
}
catch (Exception ex)
{
string err = $"An error has occured: \n{ex.Message}";
_ = MessageBox.Show(err);
}
}
}
}