2023-10-15 12:13:48 -07:00

187 lines
6.9 KiB
C#

using Microsoft.Extensions.Configuration;
using Phares.Shared;
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;
using View_by_Distance.Drag.Drop.Move.Models;
using View_by_Distance.Shared.Models.Stateless.Methods;
using WindowsShortcutFactory;
namespace View_by_Distance.Drag.Drop.Move;
public partial class DragDropMove : Form
{
private readonly TextBox _PathTextBox;
private readonly TextBox _JsonTextBox;
private readonly TextBox _FirstTextBox;
private readonly AppSettings _AppSettings;
private readonly ProgressBar _ProgressBar;
private readonly string _WorkingDirectory;
private readonly IsEnvironment _IsEnvironment;
public DragDropMove()
{
InitializeComponent();
AppSettings appSettings;
string workingDirectory;
IsEnvironment isEnvironment;
IConfigurationRoot configurationRoot;
Assembly assembly = Assembly.GetExecutingAssembly();
bool debuggerWasAttachedAtLineZero = Debugger.IsAttached || assembly.Location.Contains(@"\bin\Debug");
isEnvironment = new(processesCount: null, nullASPNetCoreEnvironmentIsDevelopment: debuggerWasAttachedAtLineZero, nullASPNetCoreEnvironmentIsProduction: !debuggerWasAttachedAtLineZero);
IConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(isEnvironment.AppSettingsFileName, optional: false, reloadOnChange: true)
.AddUserSecrets<Program>();
configurationRoot = configurationBuilder.Build();
appSettings = Models.Binder.AppSettings.Get(configurationRoot);
if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName))
throw new Exception("Working path name must have parentDirectory value!");
workingDirectory = IWorkingDirectory.GetWorkingDirectory(assembly.GetName().Name, appSettings.WorkingDirectoryName);
Environment.SetEnvironmentVariable(nameof(workingDirectory), workingDirectory);
_AppSettings = appSettings;
_IsEnvironment = isEnvironment;
_WorkingDirectory = workingDirectory;
_ProgressBar = new() { TabIndex = 4, Location = new(5, 5), Dock = DockStyle.Top, Visible = false };
string json = JsonSerializer.Serialize(_AppSettings, new JsonSerializerOptions { WriteIndented = true });
_FirstTextBox = new() { TabIndex = 1, Text = _IsEnvironment.Profile, Location = new(5, 5), Dock = DockStyle.Top };
_PathTextBox = new() { TabIndex = 2, Text = _AppSettings.WorkingDirectoryName, Location = new(5, 5), Dock = DockStyle.Top };
_JsonTextBox = new() { TabIndex = 3, Text = json, Multiline = true, MinimumSize = new(1, 80), Location = new(5, 5), Dock = DockStyle.Top };
Load += new EventHandler(Form1_Load);
Controls.Add(_ProgressBar);
Controls.Add(_JsonTextBox);
Controls.Add(_PathTextBox);
Controls.Add(_FirstTextBox);
}
private void Form1_Load(object? sender, EventArgs e)
{
try
{
AllowDrop = true;
DragDrop += new DragEventHandler(Form1_DragDrop);
DragEnter += new DragEventHandler(Form1_DragEnter);
_FirstTextBox.LostFocus += new EventHandler(TextBox_LostFocus);
_PathTextBox.LostFocus += new EventHandler(TextBox_LostFocus);
if (_WorkingDirectory is null)
{ }
}
catch (Exception)
{
throw;
}
}
private static string GetConverted(string value)
{
string result = value.Length < 2 || value[1] != ':' ? value : value.Replace("\\\\", "/").Replace('\\', '/');
return result;
}
private void TextBox_LostFocus(object? sender, EventArgs e)
{
try
{
if (sender is TextBox textBox)
{
textBox.Text = GetConverted(textBox.Text);
if (textBox.Text == "315360000000000")
throw new NotImplementedException();
}
}
catch (Exception)
{
throw;
}
}
private void Form1_DragEnter(object? sender, DragEventArgs e)
{
try
{
if (e.Data is not null && e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
catch (Exception)
{
throw;
}
}
private void MovePaths(string[] paths)
{
string file;
string checkFile;
string? directory;
string? relativePath;
string[] directories;
string? checkDirectory;
string? shellDirectory;
int rootDirectoryLength;
WindowsShortcut windowsShortcut;
_PathTextBox.Text = string.Empty;
_FirstTextBox.Text = GetConverted(paths[0]);
foreach (string path in paths.OrderBy(l => l))
{
relativePath = null;
shellDirectory = null;
if (!path.EndsWith(".lnk"))
file = path;
else
{
windowsShortcut = WindowsShortcut.Load(path);
if (windowsShortcut.Path is null)
continue;
file = windowsShortcut.Path;
windowsShortcut.Dispose();
}
directory = Path.GetDirectoryName(file);
if (string.IsNullOrEmpty(directory))
continue;
directories = IPath.GetDirectories(directory);
foreach (string directoryName in directories)
{
checkDirectory = $"{directoryName}-Shell";
if (!Directory.Exists(checkDirectory))
continue;
shellDirectory = checkDirectory;
rootDirectoryLength = directoryName.Length;
relativePath = file[rootDirectoryLength..];
break;
}
if (string.IsNullOrEmpty(shellDirectory) || string.IsNullOrEmpty(relativePath))
continue;
checkFile = string.Concat(shellDirectory, relativePath);
checkDirectory = Path.GetDirectoryName(checkFile);
if (string.IsNullOrEmpty(checkDirectory))
continue;
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
if (File.Exists(checkFile))
continue;
File.Move(file, checkFile);
}
}
private void Form1_DragDrop(object? sender, DragEventArgs e)
{
try
{
if (e.Data is not null && e.Data.GetData(DataFormats.FileDrop) is string[] paths && paths.Length > 0)
MovePaths(paths);
else
{
_FirstTextBox.Text = string.Empty;
_PathTextBox.Text = string.Empty;
_JsonTextBox.Text = string.Empty;
}
}
catch (Exception)
{
throw;
}
}
}