Skip to content

Commit

Permalink
Removed cmd line code and added interface code
Browse files Browse the repository at this point in the history
The cmd line application can still be downloaded (versions 0.0.3 and below), now we are providing the updated code with the interface supplied.
  • Loading branch information
mwd1993 authored Mar 27, 2022
1 parent 41044cc commit b1071ea
Show file tree
Hide file tree
Showing 8 changed files with 646 additions and 20 deletions.
4 changes: 2 additions & 2 deletions DesktopSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DesktopSorter
{
public static class DesktopSorter
{
public static void Main(string[] args)
public static void secondaryMain(string[] args)
{
if (args.Length >= 1)
{
Expand All @@ -26,7 +26,7 @@ public static void Main(string[] args)
{
case "test":
{
DesktopSorterFolders.compressFolders();
// Nothing for now
break;
}
case "compress":
Expand Down
19 changes: 17 additions & 2 deletions DesktopSorterFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ public static List<string> compressFiles()
{
if (Path.GetExtension(f).Replace(".", "").Trim() == _f)
{
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/" + _f + "/" + Path.GetFileName(f));
try
{
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/" + _f + "/" + Path.GetFileName(f));
}
catch
{
// file/folder in use, so we can't move it
}

break;
}
}
Expand All @@ -160,7 +168,14 @@ public static List<string> compressFiles()
string file_name = Path.GetFileName(f);
string file_extension = Path.GetExtension(f).Replace(".", "").Trim();
Directory.CreateDirectory(DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + file_extension);
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + file_extension + "/" + file_name);
try
{
File.Move(f, DesktopSorterVariables.desktopPath + "/DesktopSorter/_PublicDesktop/" + file_extension + "/" + file_name);
}
catch
{
// file in use
}
}
#endregion

Expand Down
23 changes: 18 additions & 5 deletions DesktopSorterFolders.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DesktopSorter
{
Expand Down Expand Up @@ -73,13 +71,28 @@ public static List<string> compressFolders()
public static List<string> decompressFolders()
{
List<string> foldersDecompressed = new List<string>();
string[] folders = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/");\
string[] folders = new string[] { };
try
{
folders = Directory.GetDirectories(DesktopSorterVariables.desktopPath + "/DesktopSorter/_DesktopSorterFolders/");
}
catch
{
return foldersDecompressed;
}
// Loop through each directory
// in our compression directory
foreach (var f in folders)
{
// Move the file back to the desktop
Directory.Move(f, DesktopSorterVariables.desktopPath + "/" + Path.GetFileName(f));
try
{
Directory.Move(f, DesktopSorterVariables.desktopPath + "/" + Path.GetFileName(f));
}
catch
{
// folder has something in use by user, cannot move
}
foldersDecompressed.Add(f);
}
return foldersDecompressed;
Expand Down
144 changes: 144 additions & 0 deletions DesktopSorterMain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using DesktopSorter;
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace DesktopSorterWF
{
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;
// MessageBox.Show(currTimeMS + " - " + Interface.lvLoadLastTimeoutValue + " > " + Interface.lvLoadTimeoutValue + "?");
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 (DesktopSorterWF.Interface.filesDetected.Contains(fn))
continue;
var listViewItem = new ListViewItem(new string[] { fn });
Interface.lv1.Items.Add(listViewItem);
Interface.filesDetected.Add(fn);
// Interface.lv1L.Text = "Size: " + DesktopSorterUtilities.directorySize(DesktopSorterVariables.desktopPath);
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();
}
}
}

}));

// Start our thread
thread.Start();

Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Interface());

}

}
}
37 changes: 28 additions & 9 deletions DesktopSorterUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,36 @@ namespace DesktopSorter
{
class DesktopSorterUtilities
{
public static float desktopSize()
{
string[] files = Directory.GetFiles(DesktopSorterVariables.desktopPath);
float desktopSize = 0;
foreach(var f in files)
{
FileInfo fileInfo = new FileInfo(f);
desktopSize += fileInfo.Length;
}
return desktopSize;
}

public static float directorySize(string directory, bool inGigabytes = true)
{
float size = 0;
DirectoryInfo d = new DirectoryInfo(directory);
FileInfo[] fis = d.GetFiles();
DirectoryInfo[] dis = d.GetDirectories();
foreach (FileInfo fi in fis)
size += fi.Length;
foreach (DirectoryInfo di in dis)
size += directorySize(di.FullName, inGigabytes: false);
return size;
try
{
float size = 0;
DirectoryInfo d = new DirectoryInfo(directory);
FileInfo[] fis = d.GetFiles();
DirectoryInfo[] dis = d.GetDirectories();
foreach (FileInfo fi in fis)
size += fi.Length;
foreach (DirectoryInfo di in dis)
size += directorySize(di.FullName, inGigabytes: false);
return size;
}
catch
{
return 0;
}
}

public static String bytesToString(long byteCount)
Expand Down
4 changes: 2 additions & 2 deletions DesktopSorterVariables.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DesktopSorter
{
Expand Down Expand Up @@ -30,7 +29,8 @@ public static class DesktopSorterVariables
public static List<string> ignoreFiles = new List<string>()
{
"desktopsorter.exe",
"desktop.ini"
"desktop.ini",
"desktopSorterWF.exe"
};
public static List<string> errorAccessDenied = new List<string>()
{
Expand Down
Loading

0 comments on commit b1071ea

Please sign in to comment.