mirror of
https://github.com/Hofknecht/SystemTrayMenu.git
synced 2024-10-01 01:27:33 +13:00
51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
|
// <copyright file="GenerateDriveShortcuts.cs" company="PlaceholderCompany">
|
|||
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|||
|
// </copyright>
|
|||
|
|
|||
|
namespace SystemTrayMenu.Utilities
|
|||
|
{
|
|||
|
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]);
|
|||
|
string linkPath = GetLinkPathFromDriveName(driveInfo.Name.Substring(0, 1));
|
|||
|
if (!System.IO.File.Exists(linkPath))
|
|||
|
{
|
|||
|
CreateShortcut(linkPath, driveInfo.Name);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
foreach (char driveName in driveNamesToRemove)
|
|||
|
{
|
|||
|
string possibleShortcut = GetLinkPathFromDriveName(driveName.ToString());
|
|||
|
System.IO.File.Delete(possibleShortcut);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private static void CreateShortcut(string linkPath, string targetPath)
|
|||
|
{
|
|||
|
WshShell shell = new WshShell();
|
|||
|
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(linkPath);
|
|||
|
shortcut.Description = "Generated by SystemTrayMenu";
|
|||
|
shortcut.TargetPath = targetPath;
|
|||
|
shortcut.Save();
|
|||
|
}
|
|||
|
|
|||
|
private static string GetLinkPathFromDriveName(string driveName)
|
|||
|
{
|
|||
|
return Path.Combine(Config.Path, $" {driveName} .lnk");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|