Example #1
0
        public void Save(string linkFileName)
        {
            if (null == linkFileName)
            {
                throw new ArgumentNullException("linkFileName", "A name of the link file cannot be null");
            }
            new FileIOPermission(FileIOPermissionAccess.Write, linkFileName).Demand();

            int    showCmd = (int)this.showCmd;
            object sl      = null;

            try
            {
                Type slType = Type.GetTypeFromCLSID(CLSID_ShellLink);
                sl = Activator.CreateInstance(slType);
                IShellLinkW shellLinkW = sl as IShellLinkW;
                if (null == shellLinkW)
                {
                    IShellLinkA shellLinkA = sl as IShellLinkA;
                    if (null == shellLinkA)
                    {
                        ThrowInvalidComObjectException();
                    }
                    shellLinkA.SetArguments(Str(this.arguments));
                    shellLinkA.SetDescription(Str(this.description));
                    shellLinkA.SetHotkey(this.hotkey);
                    shellLinkA.SetIconLocation(Str(this.iconPath), this.iconIndex);
                    shellLinkA.SetPath(Str(this.path));
                    shellLinkA.SetShowCmd(showCmd);
                    shellLinkA.SetWorkingDirectory(Str(this.workingDirectory));
                }
                else
                {
                    shellLinkW.SetArguments(Str(this.arguments));
                    shellLinkW.SetDescription(Str(this.description));
                    shellLinkW.SetHotkey(this.hotkey);
                    shellLinkW.SetIconLocation(Str(this.iconPath), this.iconIndex);
                    shellLinkW.SetPath(Str(this.path));
                    shellLinkW.SetShowCmd(showCmd);
                    shellLinkW.SetWorkingDirectory(Str(this.workingDirectory));
                }

                IPersistFile pf = sl as IPersistFile;
                pf.Save(linkFileName, true);
            }
            finally
            {
                if (null != sl)
                {
                    Marshal.ReleaseComObject(sl);
                }
            }
            // This object is not eligible for the garbage collection during this method call
            GC.KeepAlive(this);
        }
 /// <summary>
 /// アイコンのファイルとインデックスを設定する
 /// </summary>
 /// <param name="iconFile">アイコンのファイル</param>
 /// <param name="iconIndex">アイコンのインデックス</param>
 private void SetIconLocation(string iconFile, int iconIndex)
 {
     if (isUnicodeEnvironment)
     {
         shellLinkW.SetIconLocation(iconFile, iconIndex);
     }
     else
     {
         shellLinkA.SetIconLocation(iconFile, iconIndex);
     }
 }