Example #1
0
        public void Load(string linkFileName)
        {
            if (null == linkFileName)
            {
                throw new ArgumentNullException("linkFileName", "A name of the link file cannot be null");
            }
            if (!File.Exists(linkFileName))
            {
                throw new FileNotFoundException("Link not found", linkFileName);
            }
            new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, linkFileName).Demand();

            object sl = null;

            try
            {
                Type slType = Type.GetTypeFromCLSID(CLSID_ShellLink);
                sl = Activator.CreateInstance(slType);
                IPersistFile pf = sl as IPersistFile;
                pf.Load(linkFileName, 0);

                int           showCmd;
                StringBuilder builder    = new StringBuilder(INFOTIPSIZE);
                IShellLinkW   shellLinkW = sl as IShellLinkW;
                if (null == shellLinkW)
                {
                    IShellLinkA shellLinkA = sl as IShellLinkA;
                    if (null == shellLinkA)
                    {
                        ThrowInvalidComObjectException();
                    }
                    shellLinkA.GetArguments(builder, builder.Capacity);
                    this.arguments = builder.ToString();
                    shellLinkA.GetDescription(builder, builder.Capacity);
                    this.description = builder.ToString();
                    shellLinkA.GetHotkey(out this.hotkey);
                    shellLinkA.GetIconLocation(builder, builder.Capacity, out this.iconIndex);
                    this.iconPath = builder.ToString();
                    Win32FindDataA wfd;
                    shellLinkA.GetPath(builder, builder.Capacity, out wfd, SLGP_UNCPRIORITY);
                    this.path = builder.ToString();
                    shellLinkA.GetShowCmd(out showCmd);
                    shellLinkA.GetWorkingDirectory(builder, builder.Capacity);
                    this.workingDirectory = builder.ToString();
                }
                else
                {
                    shellLinkW.GetArguments(builder, builder.Capacity);
                    this.arguments = builder.ToString();
                    shellLinkW.GetDescription(builder, builder.Capacity);
                    this.description = builder.ToString();
                    shellLinkW.GetHotkey(out this.hotkey);
                    shellLinkW.GetIconLocation(builder, builder.Capacity, out this.iconIndex);
                    this.iconPath = builder.ToString();
                    Win32FindDataW wfd;
                    shellLinkW.GetPath(builder, builder.Capacity, out wfd, SLGP_UNCPRIORITY);
                    this.path = builder.ToString();
                    shellLinkW.GetShowCmd(out showCmd);
                    shellLinkW.GetWorkingDirectory(builder, builder.Capacity);
                    this.workingDirectory = builder.ToString();
                }
                this.showCmd = (ShowCommand)showCmd;
            }
            finally
            {
                if (null != sl)
                {
                    Marshal.ReleaseComObject(sl);
                }
            }
            // This object is not eligible for the garbage collection during this method call
            GC.KeepAlive(this);
        }