Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 67 additions & 22 deletions WindowsTileCustomizer/Core/StartMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,73 @@ public StartMenu(string Path)
this._Path = Path;
}

public string Path {
public string Path
{
get { return this._Path; }
}

public string Name {
get {
public string Name
{
get
{
return System.IO.Path.GetFileNameWithoutExtension(Path);
}
}

public string Target {
get {
public string Target
{
get
{
Shell WShell = new Shell();
Folder ShortcutFolder = WShell.NameSpace(System.IO.Path.GetDirectoryName(Path));
FolderItem Shortcut = ShortcutFolder.ParseName(System.IO.Path.GetFileName(Path));
try {
if (Shortcut != null && Shortcut.IsLink) {
try
{
if (Shortcut != null && Shortcut.IsLink)
{
ShellLinkObject ShortcutLink = (ShellLinkObject)Shortcut.GetLink;
string ShortcutLinkPath = ShortcutLink.Path;
if (string.IsNullOrEmpty(ShortcutLinkPath)) return null;
if (!File.Exists(ShortcutLinkPath)) return ShortcutLinkPath.Replace("\\Program Files (x86)\\", "\\Program Files\\");
return File.Exists(ShortcutLinkPath) ? ShortcutLinkPath : null;
}
} catch (UnauthorizedAccessException e) {
}
catch (UnauthorizedAccessException e)
{
System.Diagnostics.Debug.WriteLine($"{e.Message}: {Name}");
}

return null;
}
}

public string TargetDirectory {
get {
public string TargetDirectory
{
get
{
return Target != null ? System.IO.Path.GetDirectoryName(Target) : null;
}
}

public Bitmap Icon {
get {
try {
public Bitmap Icon
{
get
{
try
{
return Target != null ? System.Drawing.Icon.ExtractAssociatedIcon(Target).ToBitmap() : null;
} catch (Exception) {
}
catch (Exception)
{
return null;
}
}
}

public string ManifestPath {
get {
public string ManifestPath
{
get
{
if (string.IsNullOrEmpty(Target)) return null;
return System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(Target),
Expand All @@ -86,15 +104,42 @@ public void Refresh()
Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu)
};

public static IEnumerable<StartMenu> GetAllItems()
public static ICollection<StartMenu> GetAllItems()
{
foreach (var path in Paths) {
foreach (var shortcut in Directory.GetFiles(path, "*.lnk", SearchOption.AllDirectories)) {
StartMenu menuItem = new StartMenu(shortcut);
if (!string.IsNullOrEmpty(menuItem.Target)) {
yield return menuItem;
var result = new List<StartMenu>();
foreach (var path in Paths)
{
ReadAllItems(path, result);
}
return result;
}

public static void ReadAllItems(string path, ICollection<StartMenu> items)
{
// in order to skip directories which throw exception, perform recursively starting with directories and then files
foreach (var directory in Directory.EnumerateDirectories(path))
{
try
{
ReadAllItems(directory, items);
}
catch (UnauthorizedAccessException)
{
}
}
foreach (var shortcut in Directory.EnumerateFiles(path, "*.lnk", SearchOption.TopDirectoryOnly))
{
try
{
var menuItem = new StartMenu(shortcut);
if (!string.IsNullOrEmpty(menuItem.Target))
{
items.Add(menuItem);
}
}
catch (UnauthorizedAccessException)
{
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion WindowsTileCustomizer/Forms/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Main()

private void LoadStartMenuItems()
{
StartMenuItems = StartMenu.GetAllItems().ToArray().OrderBy(i => i.Name).ToArray();
StartMenuItems = StartMenu.GetAllItems().OrderBy(i => i.Name).ToArray();
StartMenuDropdown.DataSource = StartMenuItems;
}

Expand Down