|
using System;
using IWshRuntimeLibrary;
using Shell32;
using System.IO;
using System.Windows.Forms;
using MessageManager;
namespace Desktop_Web_Stats
{
public class Manage
{
public static string lnkName = "Mega Lotto Web Stats";
public static void StartupFolderLnk()
{
Properties.Settings settings = Properties.Settings.Default;
if (settings.autoStart)
{
if (!StartUpFolderShortCutExists())
{
try
{
CreateStartupFolderShortcut();
}
catch
{
Dialog.Message("Error", "The Startup Folder Shortcut could not be created.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
if (!settings.autoStart)
{
if (StartUpFolderShortCutExists())
{
try
{
DeleteStartupFolderShortcuts(Path.GetFileName(Application.ExecutablePath));
}
catch { }
}
}
}
private static bool StartUpFolderShortCutExists()
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string appExeName = Path.GetFileName(Application.ExecutablePath);
DirectoryInfo folder = new DirectoryInfo(startUpFolderPath);
FileInfo[] files = folder.GetFiles("*.lnk");
foreach (FileInfo file in files)
{
string shortcutTargetFile = GetShortcutTargetFile(file.FullName);
if (shortcutTargetFile.EndsWith(appExeName, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
return false;
}
private static void CreateStartupFolderShortcut()
{
WshShellClass wshShell = new WshShellClass();
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell
.CreateShortcut(startUpFolderPath + "\\" + Application.ProductName + ".lnk");
shortcut.TargetPath = Application.ExecutablePath;
shortcut.WorkingDirectory = Application.StartupPath;
shortcut.Description = lnkName;
shortcut.Save();
}
private static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = Path.GetDirectoryName(shortcutFilename);
string filenameOnly = Path.GetFileName(shortcutFilename);
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(pathOnly);
Shell32.FolderItem fileName = folder.ParseName(filenameOnly);
if (fileName != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)fileName.GetLink;
return link.Path;
}
return String.Empty;
}
private static void DeleteStartupFolderShortcuts(string targetExeName)
{
string startUpFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
DirectoryInfo folder = new DirectoryInfo(startUpFolderPath);
FileInfo[] files = folder.GetFiles("*.lnk");
foreach (FileInfo file in files)
{
string shortcutTargetFile = GetShortcutTargetFile(file.FullName);
if (shortcutTargetFile.EndsWith(targetExeName, StringComparison.InvariantCultureIgnoreCase))
{
System.IO.File.Delete(file.FullName);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.