-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopSorterMain.cs
140 lines (137 loc) · 7.26 KB
/
DesktopSorterMain.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
using DesktopSorter;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace DesktopSorterWF
{
/// <summary>
/// DesktopSorter class that also
/// handles the main entry point
/// </summary>
static class DesktopSorterMain
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Create a thread to keep our list views up to date
// with our desktop and the sorted directory folder names
Thread thread = new Thread(new ThreadStart(delegate ()
{
while (true)
{
if(Interface.lvLoadTimeout)
{
long currTimeMS = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
if (currTimeMS - Interface.lvLoadLastTimeoutValue > Interface.lvLoadTimeoutValue)
{
Interface.lvLoadTimeout = false;
Interface.lvLoadLastTimeoutValue = currTimeMS;
Interface.enableListViews();
}
else
continue;
}
string dp = DesktopSorterVariables.desktopPath;
string[] files = Directory.GetFiles(DesktopSorterVariables.desktopPath);
string[] desktopSorterDirectories = new string[] { };
if (Directory.Exists(DesktopSorterVariables.desktopPath + "\\DesktopSorter"))
desktopSorterDirectories = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "\\DesktopSorter");
if (Interface.lv1 != null)
{
Thread.Sleep(Interface.lvRefreshSleepTime);
// Try to modify the list views, if we fail, it's because
// the graphical interface has been closed, so we catch
// the exception, and simply exit, closing our thread aswell
try
{
Interface.lv1.Invoke((MethodInvoker)(() =>
{
if (files.Length != Interface.filesDetected.Count)
{
Interface.lv1.Clear();
Interface.filesDetected.Clear();
}
foreach (var f in files)
{
var fn = Path.GetFileName(f);
if (Interface.filesDetected.Contains(fn))
continue;
var listViewItem = new ListViewItem(new string[] { fn });
Interface.lv1.Items.Add(listViewItem);
Interface.filesDetected.Add(fn);
Thread getSizeOfDirectory = new Thread(new ThreadStart(delegate ()
{
var sizeOf = DesktopSorterUtilities.desktopSize();
var sizeOfFinal = DesktopSorterUtilities.bytesToString((long)sizeOf);
Interface.lv1L.Invoke((MethodInvoker)(() =>
{
Interface.lv1L.Text = "Size: " + sizeOfFinal + " (Folders Exluded)";
}));
}));
getSizeOfDirectory.Start();
}
}));
// If user has ran the sorter, we can proceed to
// modify the sorted directories' list view
if (desktopSorterDirectories.Length > 0)
{
Interface.lv2.Invoke((MethodInvoker)(() =>
{
// Something changed, so we can
// proceed to update the list view
if (desktopSorterDirectories.Length != Interface.desktopSorterDirsDetected.Count)
{
Interface.lv2.Clear();
Interface.desktopSorterDirsDetected.Clear();
}
foreach (var d in desktopSorterDirectories)
{
var dn = Path.GetFileName(d);
if (Interface.desktopSorterDirsDetected.Contains(dn))
continue;
var listViewItem = new ListViewItem(new string[] { dn });
Interface.lv2.Items.Add(listViewItem);
Interface.desktopSorterDirsDetected.Add(dn);
Thread getSizeOfDirectory = new Thread(new ThreadStart(delegate ()
{
var sizeOf = Double.Parse(
DesktopSorterUtilities.directorySize(DesktopSorterVariables.desktopPath + "\\DesktopSorter").ToString(),
System.Globalization.NumberStyles.Float
);
var sizeOfFinal = DesktopSorterUtilities.bytesToString((long)sizeOf);
Interface.lv2L.Invoke((MethodInvoker)(() =>
{
Interface.lv2L.Text = "Size: " + sizeOfFinal;
}));
}));
getSizeOfDirectory.Start();
}
}));
}
else
{
Interface.lv2.Invoke((MethodInvoker)(() =>
{
Interface.lv2.Items.Clear();
}));
}
}
catch
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
}
}
}));
thread.Start();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Interface());
}
}
}