Exemplo n.º 1
0
        public List <LinkType> LoadLinksFromStartup()
        {
            DirectoryInfo di = new DirectoryInfo(GetStartupPath());

            FileInfo[]      files       = di.GetFiles("*DelayStartup.lnk");
            List <LinkType> listOfLinks = new List <LinkType>();

            foreach (FileInfo fi in files)
            {
                //parse link into string
                string pathOnly     = Path.GetDirectoryName(fi.FullName);
                string filenameOnly = Path.GetFileName(fi.FullName);

                Shell32.Shell      shell      = new Shell32.ShellClass();
                Shell32.Folder     folder     = shell.NameSpace(pathOnly);
                Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);

                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    LinkType a = new LinkType(link, fi);
                    listOfLinks.Add(a);
                }
            }
            return(listOfLinks);
        }
Exemplo n.º 2
0
            private static string GetShortcutTargetFile(string shortcutFilename)
            {
                string pathOnly     = Path.GetDirectoryName(shortcutFilename);
                string filenameOnly = Path.GetFileName(shortcutFilename);

                Type ShellAppType = Type.GetTypeFromProgID("Shell.Application");

                if (ShellAppType != null)
                {
                    Shell32.Shell shell = Activator.CreateInstance(ShellAppType) as Shell32.Shell;
                    if (shell != null)
                    {
                        Shell32.Folder folder = shell.NameSpace(pathOnly);
                        if (folder != null)
                        {
                            Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
                            if (folderItem != null)
                            {
                                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                                return(link.Path);
                            }
                        }
                    }
                }
                return(String.Empty); // Not found
            }
Exemplo n.º 3
0
        private void RemoveAutoUpdator()
        {
            string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\autoupdator.exe.lnk";

            if (File.Exists(startupFolderPath))
            {
                Process.GetProcessesByName("autoupdator").ToList().ForEach(pr => pr.Kill());
                string pathOnly     = Path.GetDirectoryName(startupFolderPath);
                string filenameOnly = Path.GetFileName(startupFolderPath);
                string targetPath   = "";

                Shell      shell      = new Shell();
                Folder     folder     = shell.NameSpace(pathOnly);
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    targetPath = link.Path;
                }

                try
                {
                    Directory.Delete(Directory.GetParent(targetPath).ToString(), true);
                    TerminalLogger.Instance.Write("AutoUpdator and all subdirectories has been Successfully Removed!");
                }
                catch (Exception e)
                {
                    ErrorLogger.Instance.Write("Failed to Remove AutoUpdator. Exception: " + e);
                }
            }
        }
Exemplo n.º 4
0
        /*************************************************************************************************************************/
        // SHORTCUTS

        /// <summary>
        /// get path from lnk file in windows  </summary>
        public static string[] GetShortcutTargetFile(string shortcutFilename)
        {
            try
            {
                string pathOnly     = Os.GetDirectoryName(shortcutFilename);
                string filenameOnly = Os.GetFileName(shortcutFilename);

                Shell      shell      = new Shell();
                Folder     folder     = shell.NameSpace(pathOnly);
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

                    if (link.Arguments != "")
                    {
                        return(new string[] { link.Path, link.Arguments });
                    }

                    return(new string[] { link.Path, "" });
                }
            }
            catch (Exception e)
            {
                Program.log.Write("GetShortcutTargetFile error: " + e.Message);
            }

            return(new string[] { "", "" });
        }
        // LNK parser function
        static string getPathFromLnk(string lnkPath)
        {
            // create instance of Shell.Application COM
            Type    t     = Type.GetTypeFromProgID("Shell.Application");
            dynamic shell = Activator.CreateInstance(t);

            // https://docs.microsoft.com/en-us/windows/win32/shell/nse-works
            // "Behind the scenes, every folder that Windows Explorer displays is represented by
            // a Component Object Model(COM) object called a folder object.Each time the user interacts with
            // a folder or its contents, the Shell communicates with the associated folder object through
            // one of a number of standard interfaces."

            // get the folder object from LNK path
            Folder f = shell.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));

            // get the folder item (LNK file)
            FolderItem fi = f.ParseName(System.IO.Path.GetFileName(lnkPath));

            if (fi != null)
            {
                // extract path from LNK file
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)fi.GetLink;
                return(link.Path);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 6
0
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            string pathOnly     = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

            Shell      shell      = new Shell();
            Folder     folder     = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);

            try
            {
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    return(link.Path);
                }
            } catch (Exception e)
            {
                _log.Error("Could not get the Target file of Shortcut: " + e.ToString());
                return(string.Empty);
            }


            return(string.Empty);
        }
Exemplo n.º 7
0
        //http://csharphelper.com/blog/2018/06/get-information-about-windows-shortcuts-in-c/
        internal static string GetShortcutInfo(string full_name,
                                               out string name, out string path, out string descr,
                                               out string working_dir, out string args)
        {
            name        = "";
            path        = "";
            descr       = "";
            working_dir = "";
            args        = "";
            try
            {
                // Make a Shell object.
                Shell32.Shell shell = new Shell32.Shell();

                // Get the shortcut's folder and name.
                string shortcut_path =
                    full_name.Substring(0, full_name.LastIndexOf("\\"));
                string shortcut_name =
                    full_name.Substring(full_name.LastIndexOf("\\") + 1);
                if (!shortcut_name.EndsWith(".lnk"))
                {
                    shortcut_name += ".lnk";
                }

                // Get the shortcut's folder.
                Shell32.Folder shortcut_folder =
                    shell.NameSpace(shortcut_path);

                // Get the shortcut's file.
                Shell32.FolderItem folder_item =
                    shortcut_folder.Items().Item(shortcut_name);

                if (folder_item == null)
                {
                    return("Cannot find shortcut file '" + full_name + "'");
                }
                if (!folder_item.IsLink)
                {
                    return("File '" + full_name + "' isn't a shortcut.");
                }

                // Display the shortcut's information.
                Shell32.ShellLinkObject lnk =
                    (Shell32.ShellLinkObject)folder_item.GetLink;
                name        = folder_item.Name;
                descr       = lnk.Description;
                path        = lnk.Path;
                working_dir = lnk.WorkingDirectory;
                args        = lnk.Arguments;
                return("");
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 8
0
 public LinkType(Shell32.ShellLinkObject _Link,FileInfo _FileInfo)
 {
     Link = _Link;
     FileInfo = _FileInfo;
     Arguments = Link.Arguments;
     WorkingDirectory = Link.WorkingDirectory;
     Description = Link.Description;
     Target = Link.Target.Path;
     LinkFullName = FileInfo.FullName;
     //Path = Link.Path;
 }
Exemplo n.º 9
0
 public LinkType(Shell32.ShellLinkObject _Link, FileInfo _FileInfo)
 {
     Link             = _Link;
     FileInfo         = _FileInfo;
     Arguments        = Link.Arguments;
     WorkingDirectory = Link.WorkingDirectory;
     Description      = Link.Description;
     Target           = Link.Target.Path;
     LinkFullName     = FileInfo.FullName;
     //Path = Link.Path;
 }
Exemplo n.º 10
0
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            string     pathOnly     = System.IO.Path.GetDirectoryName(shortcutFilename);
            string     filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
            Shell      shell        = new Shell();
            Folder     folder       = shell.NameSpace(pathOnly);
            FolderItem folderItem   = folder.ParseName(filenameOnly);

            if (folderItem != null)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                return(link.Path);
            }
            return(string.Empty);
        }
Exemplo n.º 11
0
        public static string GetShortcutTargetFile(string shortcutFilename)
        {
            string directory = Path.GetDirectoryName(shortcutFilename);
            string filename  = Path.GetFileName(shortcutFilename);

            S32.Shell      shell  = new S32.ShellClass();
            S32.Folder     folder = shell.NameSpace(directory);
            S32.FolderItem item   = folder.ParseName(filename);

            if (item == null)
            {
                return(String.Empty);
            }

            S32.ShellLinkObject link = (S32.ShellLinkObject)item.GetLink;
            return(link.Path);
        }
Exemplo n.º 12
0
        public static string GetWindowsShortcutTargetFileArguments(string shortcutFilename)
        {
            string pathOnly     = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);

            Shell32.Shell shell      = new Shell32.Shell();
            Folder        folder     = shell.NameSpace(pathOnly);
            FolderItem    folderItem = folder.ParseName(filenameOnly);

            if (folderItem != null)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                string args = link.Arguments.Trim('"');
                return(args);
            }

            return(string.Empty);
        }
Exemplo n.º 13
0
        /// <summary>
        ///get icon from lnk file in windows  </summary>
        public static string GetShortcutIcon(String shortcutFilename)
        {
            String pathOnly     = Os.GetDirectoryName(shortcutFilename);
            String filenameOnly = Os.GetFileName(shortcutFilename);

            Shell      shell      = new Shell();
            Folder     folder     = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);

            if (folderItem != null)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                link.GetIconLocation(out String iconlocation);
                return(iconlocation);
            }

            return(String.Empty);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Checks wheter if the shortcut is created.
        /// </summary>
        /// <returns></returns>
        public static bool IsStartupCreated() //If there is a shortcut called WallpaperClock and it's target is current directory.
        {
            Shell shell = new Shell();

            if (File.Exists(startupPath + "\\WallpaperClock.lnk"))
            {
                Folder     folder            = shell.NameSpace(startupPath);
                FolderItem folderItem        = folder.ParseName("WallpaperClock.lnk");
                Shell32.ShellLinkObject link = folderItem.GetLink as Shell32.ShellLinkObject;


                if (string.Equals(link.Path, currentPath, StringComparison.OrdinalIgnoreCase)) //If the current shortcut's target equals current .exe's location.
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the shortcut target file.
        /// </summary>
        /// <param name="shortcutFilename">The shortcut filename.</param>
        /// <returns></returns>
        private static string GetShortcutTargetFile(string shortcutFilename)
        {
            if ((Path.GetExtension(shortcutFilename) ?? string.Empty).ToLower() != ".lnk")
            {
                return(shortcutFilename);
            }

            string pathOnly     = Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = Path.GetFileName(shortcutFilename);

            dynamic shell = null;

            try
            {
                shell = new Shell();
            }
            catch (InvalidCastException ice)
            {
                // This happens on Windows 10 machines (but not in server 2012 machines)
                _logger.Log("Failed to interface with Windows to get shortcuts. Trying again...", ice);
                Type t = Type.GetTypeFromProgID("Shell.Application");
                shell = Activator.CreateInstance(t);
            }

            Folder     folder     = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);

            if (folderItem != null)
            {
                try
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    return(link.Path);
                }
                catch
                {
                    // TODO: Get the type of Access denied exception and consume that.
                    // otherwise, throw it.
                }
            }

            return(string.Empty);
        }
Exemplo n.º 16
0
        private string getAutosyncVersion()
        {
            string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\AutoSync.exe.lnk";

            if (File.Exists(startupFolderPath))
            {
                string pathOnly     = System.IO.Path.GetDirectoryName(startupFolderPath);
                string filenameOnly = System.IO.Path.GetFileName(startupFolderPath);

                Shell      shell      = new Shell();
                Folder     folder     = shell.NameSpace(pathOnly);
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    var versionInfo = FileVersionInfo.GetVersionInfo(link.Path);
                    return(versionInfo.ProductVersion);
                }
            }
            return("");
        }
Exemplo n.º 17
0
        private static TargetDescriptor GetShortcutTargetDescriptor(string shortcutFilename)
        {
            string pathOnly     = Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = Path.GetFileName(shortcutFilename);

            Shell      shell      = new Shell();
            Folder     folder     = shell.NameSpace(pathOnly);
            FolderItem folderItem = folder.ParseName(filenameOnly);

            TargetDescriptor desc = new TargetDescriptor();

            desc.Type = TargetType.Unknown;

            if (folderItem != null)
            {
                Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;

                if (string.IsNullOrWhiteSpace(link.Path))
                {
                    return(desc);
                }
                else
                {
                    desc.Path             = link.Path;
                    desc.Extension        = Path.GetExtension(desc.Path);
                    desc.Name             = Path.GetFileName(desc.Path);
                    desc.Arguments        = link.Arguments;
                    desc.WorkingDirectory = link.WorkingDirectory;

                    desc.Type = Directory.Exists(link.Path) ? TargetType.Directory : TargetType.File;

                    return(desc);
                }
            }

            return(desc);
        }
Exemplo n.º 18
0
        //http://csharphelper.com/blog/2018/06/get-information-about-windows-shortcuts-in-c/
        // Get information about this link.
        // Return an error message if there's a problem.
        internal static string GetShortcutInfo(string full_name,
                                               out ShortcutItem vSI)
        {
            //name = "";
            //path = "";
            //descr = "";
            //working_dir = "";
            //args = "";
            //var sc = Keys.Control && Keys.LShiftKey && Keys.A;
            //var txt = new KeysConverter().ConvertToString((Keys)sc);
            //Console.WriteLine(txt);



            vSI = new ShortcutItem();

            try
            {
                // Make a Shell object.
                Shell32.Shell shell = new Shell32.Shell();

                // Get the shortcut's folder and name.
                string shortcut_path =
                    full_name.Substring(0, full_name.LastIndexOf("\\"));
                string shortcut_name =
                    full_name.Substring(full_name.LastIndexOf("\\") + 1);
                if (!shortcut_name.EndsWith(".lnk"))
                {
                    shortcut_name += ".lnk";
                }

                // Get the shortcut's folder.
                Shell32.Folder shortcut_folder =
                    shell.NameSpace(shortcut_path);

                // Get the shortcut's file.
                Shell32.FolderItem folder_item =
                    shortcut_folder.Items().Item(shortcut_name);

                if (folder_item == null)
                {
                    return("Cannot find shortcut file '" + full_name + "'");
                }
                if (!folder_item.IsLink)
                {
                    return("File '" + full_name + "' isn't a shortcut.");
                }

                // Display the shortcut's information.
                Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)folder_item.GetLink;

                vSI.Arguments   = lnk.Arguments;
                vSI.Description = lnk.Description;
                //vSI.Hotkey = lnk.Hotkey.ToString();
                lnk.GetIconLocation(out string bps);
                vSI.IconPath     = bps;
                vSI.Name         = folder_item.Name;
                vSI.ShortcutPath = lnk.Path;
                FolderItem fi = lnk.Target;
                vSI.Target = GetShortcutTargetFile(full_name);//fi.IsFolder.ToString();
                //vSI.WindowStyle = "1";
                vSI.WorkingDirectory          = lnk.WorkingDirectory;
                vSI.ShortcutPathSpecialFolder = "Other";

                WshShell    theShell    = new WshShell();
                WshShortcut theShortcut = (WshShortcut)theShell.CreateShortcut(full_name);
                vSI.WindowStyle = frmMain.WindowStyleToInt(theShortcut.WindowStyle);
                vSI.Hotkey      = theShortcut.Hotkey.ToString();

                //name = folder_item.Name;
                //descr = lnk.Description;
                //path = lnk.Path;
                //working_dir = lnk.WorkingDirectory;
                //args = lnk.Arguments;

                return("");
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }