2021-12-11 03:20:24 +13:00
|
|
|
|
// <copyright file="GenerateDriveShortcuts.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
2021-12-27 18:28:29 +13:00
|
|
|
|
using System;
|
2021-12-11 03:20:24 +13:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using IWshRuntimeLibrary;
|
|
|
|
|
|
|
|
|
|
internal class GenerateDriveShortcuts
|
|
|
|
|
{
|
|
|
|
|
public static void Start()
|
|
|
|
|
{
|
|
|
|
|
List<char> driveNamesToRemove = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray().ToList();
|
|
|
|
|
|
|
|
|
|
DriveInfo[] driveInfos = DriveInfo.GetDrives();
|
|
|
|
|
foreach (DriveInfo driveInfo in driveInfos)
|
|
|
|
|
{
|
|
|
|
|
driveNamesToRemove.Remove(driveInfo.Name[0]);
|
2021-12-27 19:35:39 +13:00
|
|
|
|
string linkPath = GetLinkPathFromDriveName(driveInfo.Name[..1]);
|
2021-12-11 03:20:24 +13:00
|
|
|
|
if (!System.IO.File.Exists(linkPath))
|
|
|
|
|
{
|
|
|
|
|
CreateShortcut(linkPath, driveInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (char driveName in driveNamesToRemove)
|
|
|
|
|
{
|
|
|
|
|
string possibleShortcut = GetLinkPathFromDriveName(driveName.ToString());
|
2022-01-30 23:26:40 +13:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
System.IO.File.Delete(possibleShortcut);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"Could not delete shortcut at path:'{possibleShortcut}'", ex);
|
|
|
|
|
}
|
2021-12-11 03:20:24 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void CreateShortcut(string linkPath, string targetPath)
|
|
|
|
|
{
|
2021-12-27 19:35:39 +13:00
|
|
|
|
WshShell shell = new();
|
2021-12-11 03:20:24 +13:00
|
|
|
|
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(linkPath);
|
|
|
|
|
shortcut.Description = "Generated by SystemTrayMenu";
|
|
|
|
|
shortcut.TargetPath = targetPath;
|
2021-12-27 18:28:29 +13:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
shortcut.Save();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"Could not create shortcut at path:'{linkPath}'", ex);
|
|
|
|
|
}
|
2021-12-11 03:20:24 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetLinkPathFromDriveName(string driveName)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(Config.Path, $" {driveName} .lnk");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|