Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Easy Desktop Web Stats

, 23 Oct 2012
Create a Windows desktop app to display daily web stats from your personal site
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael J Hill - OH

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 23 Oct 2012
Article Copyright 2012 by Michael J Hill - OH
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid