Drag-Drop-Move
This commit is contained in:
195
Drag-Drop-Move/DragDropMove.cs
Normal file
195
Drag-Drop-Move/DragDropMove.cs
Normal file
@ -0,0 +1,195 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Phares.Shared;
|
||||
using Serilog;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using View_by_Distance.Drag_Drop_Explorer.Models;
|
||||
using View_by_Distance.Shared.Models.Stateless.Methods;
|
||||
using WindowsShortcutFactory;
|
||||
|
||||
namespace View_by_Distance.Drag_Drop_Explorer;
|
||||
|
||||
public partial class DragDropMove : Form
|
||||
{
|
||||
|
||||
private readonly ILogger _Logger;
|
||||
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();
|
||||
ILogger logger;
|
||||
AppSettings appSettings;
|
||||
string workingDirectory;
|
||||
IsEnvironment isEnvironment;
|
||||
IConfigurationRoot configurationRoot;
|
||||
LoggerConfiguration loggerConfiguration = new();
|
||||
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);
|
||||
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);
|
||||
_ = ConfigurationLoggerConfigurationExtensions.Configuration(loggerConfiguration.ReadFrom, configurationRoot);
|
||||
Log.Logger = loggerConfiguration.CreateLogger();
|
||||
logger = Log.ForContext<DragDropMove>();
|
||||
logger.Information("Complete");
|
||||
_Logger = logger;
|
||||
_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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetConverted(string value)
|
||||
{
|
||||
string result = value.Length < 2 || value[1] != ':' ? value : value.Replace("\\\\", "/").Replace('\\', '/');
|
||||
return result;
|
||||
}
|
||||
|
||||
void TextBox_LostFocus(object? sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (sender is TextBox textBox)
|
||||
{
|
||||
textBox.Text = GetConverted(textBox.Text);
|
||||
if (textBox.Text == "ps")
|
||||
throw new NotImplementedException();
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Form1_DragDrop(object? sender, DragEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Data is not null && e.Data.GetData(DataFormats.FileDrop) is string[] paths && paths.Any())
|
||||
MovePaths(paths);
|
||||
else
|
||||
{
|
||||
_FirstTextBox.Text = string.Empty;
|
||||
_PathTextBox.Text = string.Empty;
|
||||
_JsonTextBox.Text = string.Empty;
|
||||
_Logger.Information("No data");
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user