private void InstallShortcut(String shortcutPath) { // Find the path to the current executable String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(_config.StaticConfig.ToastAppID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } using (PropVariant guid = new PropVariant(_config.StaticConfig.ToastActivatorCLSID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ToastActivatorCLSID, guid)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
static void InstallShortcut(string appId, string shortcutPath) { // Find the path to the current executable String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe VerifySucceeded(newShortcut.SetPath(exePath)); VerifySucceeded(newShortcut.SetArguments("")); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant applicationId = new PropVariant(appId)) { VerifySucceeded(newShortcutProperties.SetValue( SystemProperties.System.AppUserModel.ID, applicationId)); VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
/// <summary> /// Change ribbon background, highlight and text colors /// </summary> /// <param name="background">new background color</param> /// <param name="highlight">new highlight color</param> /// <param name="text">new text color</param> public void SetColors(Color background, Color highlight, Color text) { if (!Initalized) { return; } // convert colors to proper color format uint backgroundColor = ColorHelper.HSB2Uint(ColorHelper.HSL2HSB(ColorHelper.RGB2HSL(background))); uint highlightColor = ColorHelper.HSB2Uint(ColorHelper.HSL2HSB(ColorHelper.RGB2HSL(highlight))); uint textColor = ColorHelper.HSB2Uint(ColorHelper.HSL2HSB(ColorHelper.RGB2HSL(text))); IPropertyStore propertyStore = (IPropertyStore)Framework; PropVariant backgroundColorProp = PropVariant.FromObject(backgroundColor); PropVariant highlightColorProp = PropVariant.FromObject(highlightColor); PropVariant textColorProp = PropVariant.FromObject(textColor); // set ribbon colors propertyStore.SetValue(ref RibbonProperties.GlobalBackgroundColor, ref backgroundColorProp); propertyStore.SetValue(ref RibbonProperties.GlobalHighlightColor, ref highlightColorProp); propertyStore.SetValue(ref RibbonProperties.GlobalTextColor, ref textColorProp); propertyStore.Commit(); }
private static void InstallShortcut(string appIdStr, string exePath, string shortcutPath, string arguments = "") { if (File.Exists(shortcutPath)) { Console.Error.WriteLine("warning: overwriting existing shortcut"); } // Find the path to the current executable IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments(arguments)); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(appIdStr)) { ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
// T7E: Gets property store for shortcut public void SetApplicationIdForShortcut(string shortcutPath, string appId) { // Get shell item IShellItem2 shellItem; Guid guid = new Guid(ShellIIDGuid.IShellItem2); int hrc = ShellNativeMethods.SHCreateItemFromParsingName(shortcutPath, IntPtr.Zero, ref guid, out shellItem); // Get property store Guid psGuid = new Guid(ShellIIDGuid.IPropertyStore); IPropertyStore propStore = null; int hr = shellItem.GetPropertyStore( ShellNativeMethods.GETPROPERTYSTOREFLAGS.GPS_READWRITE, ref psGuid, out propStore); // Set appid property PropVariant pv = new PropVariant(); PropertyKey AppIdKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5); pv.SetString(appId); propStore.SetValue(ref AppIdKey, ref pv); propStore.Commit(); Marshal.ReleaseComObject(propStore); pv.Clear(); }
private static void CreateShortcut(string shortcutPath, string exePath) { IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); newShortcut.SetPath(exePath); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; PropVariantHelper varAppId = new PropVariantHelper(); varAppId.SetValue(Constants.AppId); newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ID, varAppId.Propvariant); PropVariantHelper varToastId = new PropVariantHelper(); varToastId.VarType = VarEnum.VT_CLSID; varToastId.SetValue(typeof(NotificationActivator).GUID); newShortcutProperties.SetValue(PROPERTYKEY.AppUserModel_ToastActivatorCLSID, varToastId.Propvariant); newShortcutProperties.Commit(); IPersistFile newShortcutSave = (IPersistFile)newShortcut; newShortcutSave.Save(shortcutPath, true); }
/// <summary> /// Gets the shell <b>IShellLink</b> representation /// of the object. /// </summary> /// <returns>An <b>IShellLink</b> up-cast to <b>object</b>.</returns> public object GetShellRepresentation() { IShellLinkW shellLink = (IShellLinkW) new CShellLink(); IPropertyStore propertyStore = (IPropertyStore)shellLink; PropVariant propVariant = new PropVariant(); if (IsSeparator) { propVariant.SetValue(true); HResult setValueResult = propertyStore.SetValue(ref PropertyKey.PKEY_AppUserModel_IsDestListSeparator, ref propVariant); setValueResult.ThrowIf(); propVariant.Clear(); propVariant.Dispose(); } else { HResult setPathResult = shellLink.SetPath(Path); setPathResult.ThrowIf(); if (!String.IsNullOrEmpty(IconLocation)) { HResult setIconLocationResult = shellLink.SetIconLocation(IconLocation, IconIndex); setIconLocationResult.ThrowIf(); } if (!String.IsNullOrEmpty(Arguments)) { HResult setArgumentsResult = shellLink.SetArguments(Arguments); setArgumentsResult.ThrowIf(); } if (!String.IsNullOrEmpty(WorkingDirectory)) { HResult setWorkingDirectoryResult = shellLink.SetWorkingDirectory(WorkingDirectory); setWorkingDirectoryResult.ThrowIf(); } HResult setShowCmdResult = shellLink.SetShowCmd((uint)ShowCommand); setShowCmdResult.ThrowIf(); propVariant.SetValue(Title); HResult setValueResult = propertyStore.SetValue(ref PropertyKey.PKEY_Title, ref propVariant); setValueResult.ThrowIf(); propVariant.Clear(); propVariant.Dispose(); } HResult commitResult = propertyStore.Commit(); commitResult.ThrowIf(); //Marshal.ReleaseComObject(propertyStore); return(shellLink); }
private static IShellLink CreateShellLink(string title, string path, string arguments, string icon_path, int icon_pos) { try { IShellLink shell_link = (IShellLink)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID.ShellLink)); shell_link.SetPath(path); if (!string.IsNullOrEmpty(arguments)) { shell_link.SetArguments(arguments); } if (!string.IsNullOrEmpty(icon_path)) { shell_link.SetIconLocation(icon_path, icon_pos); } IntPtr pps; Guid ipsiid = CLSID.IPropertyStore; Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref ipsiid, out pps); IPropertyStore property_store = (IPropertyStore)Marshal.GetTypedObjectForIUnknown(pps, typeof(IPropertyStore)); PROPVARIANT propvar = new PROPVARIANT(); propvar.SetString(title); // PKEY_Title PROPERTYKEY PKEY_Title = new PROPERTYKEY(); PKEY_Title.fmtid = new Guid("F29F85E0-4FF9-1068-AB91-08002B27B3D9"); PKEY_Title.pid = 2; property_store.SetValue(ref PKEY_Title, ref propvar); property_store.Commit(); IntPtr psl; Guid psliid = CLSID.IShellLinkW; Marshal.QueryInterface(Marshal.GetIUnknownForObject(shell_link), ref psliid, out psl); IShellLink link = (IShellLink)Marshal.GetTypedObjectForIUnknown(psl, typeof(IShellLink)); propvar.Clear(); Marshal.ReleaseComObject(property_store); property_store = null; Marshal.ReleaseComObject(shell_link); shell_link = null; return(link); } catch (COMException e) { Logger.Error("Error createing shell link: {0}\n{1}", e.Message, e.StackTrace); } return(null); }
private void StorePropVariantValue(PropVariant propVar) { var guid = new Guid(NativeAPI.Guids.Shell.IPropertyStore); IPropertyStore writablePropStore = null; try { int hr = ParentShellObject.NativeShellItem2.GetPropertyStore( GetPropertyStoreOptions.ReadWrite, ref guid, out writablePropStore); if (!CoreErrorHelper.Succeeded(hr)) { throw new PropertySystemException(LocalizedMessages.ShellPropertyUnableToGetWritableProperty, Marshal.GetExceptionForHR(hr)); } HResult result = writablePropStore.SetValue(ref propertyKey, propVar); if (!AllowSetTruncatedValue && result == HResult.InPlaceStringTruncated) { throw new ArgumentOutOfRangeException(nameof(propVar), LocalizedMessages.ShellPropertyValueTruncated); } if (!CoreErrorHelper.Succeeded(result)) { throw new PropertySystemException(LocalizedMessages.ShellPropertySetValue, Marshal.GetExceptionForHR((int)result)); } _ = writablePropStore.Commit(); } catch (InvalidComObjectException e) { throw new PropertySystemException(LocalizedMessages.ShellPropertyUnableToGetWritableProperty, e); } catch (InvalidCastException) { throw new PropertySystemException(LocalizedMessages.ShellPropertyUnableToGetWritableProperty); } finally { if (writablePropStore != null) { _ = Marshal.ReleaseComObject(writablePropStore); writablePropStore = null; } } }
public static void HashLnk(string lnkPath) { SHCreateItemFromParsingName(lnkPath, null, typeof(IShellItem2).GUID, out IShellItem item); IShellItem2 item2 = (IShellItem2)item; PSGetPropertyKeyFromName("System.Link.TargetParsingPath", out PropertyKey pk); //PKEY_Link_TargetParsingPath //pk.GUID = new Guid("{B9B4B3FC-2B51-4A42-B5D8-324146AFCF25}"); //pk.PID = 2; string targetPath; try { targetPath = item2.GetString(pk); } catch { targetPath = null; } PSGetPropertyKeyFromName("System.Link.Arguments", out pk); //PKEY_Link_Arguments //pk.GUID = new Guid("{436F2667-14E2-4FEB-B30A-146C53B5B674}"); //pk.PID = 100; string arguments; try { arguments = item2.GetString(pk); } catch { arguments = null; } string blob = GetGeneralizePath(targetPath) + arguments; blob += "do not prehash links. this should only be done by the user.";//特殊但必须存在的字符串 blob = blob.ToLower(); byte[] inBytes = Encoding.Unicode.GetBytes(blob); int byteCount = inBytes.Length; byte[] outBytes = new byte[byteCount]; HashData(inBytes, byteCount, outBytes, byteCount); uint hash = BitConverter.ToUInt32(outBytes, 0); Guid guid = typeof(IPropertyStore).GUID; IPropertyStore store = item2.GetPropertyStore(GPS.READWRITE, ref guid); PSGetPropertyKeyFromName("System.Winx.Hash", out pk); //PKEY_WINX_HASH //pk.GUID = new Guid("{FB8D2D7B-90D1-4E34-BF60-6EAC09922BBF}"); //pk.PID = 2; PropVariant pv = new PropVariant { VarType = VarEnum.VT_UI4, ulVal = hash }; store.SetValue(ref pk, ref pv); store.Commit(); Marshal.ReleaseComObject(store); Marshal.ReleaseComObject(item); PropVariantClear(pv); }
private static bool Run(string[] args) { #if DEBUG Console.WriteLine("Attempting to create IShellItem for file {0}...", args[0]); #endif IShellLink link = (IShellLink) new CShellLink(); //Win32Shell.SHCreateItemFromParsingName(args[0], IntPtr.Zero, Win32Shell.IShellLinkId, out link); IPersistFile persistFileLink = (IPersistFile)link; if (persistFileLink.Load(args[0], 0x00000002L) != 0) { Console.WriteLine("Failed to load via IPersistFile."); return(false); } link.Resolve(IntPtr.Zero, 0); /* * link.SetPath("C:\\Windows\\notepad.exe"); * link.SetArguments(""); */ #if DEBUG Console.WriteLine("Querying for IPropertyStore interface..."); #endif IPropertyStore propStore = (IPropertyStore)link; try { #if DEBUG Console.WriteLine("Attempting to set property 'System.AppUserModel.ID' to {0}...", args[1]); #endif PropertyKey appUserModelKey = new PropertyKey(new Guid("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3"), 5); propStore.SetValue(ref appUserModelKey, new BStrWrapper(args[1])); propStore.Commit(); //Store ((IPersistFile)link).Save(args[0], false); } catch (Exception ex) { #if DEBUG throw; #else Console.WriteLine("Unable to set value of AppUserModel.ID property."); Console.WriteLine(ex); return(false); #endif } return(true); }
/// <summary> /// Call this method to commit the writes (calls to WriteProperty method) /// and dispose off the writer. /// </summary> public void Close() { // Close the property writer (commit, etc) if (writablePropStore != null) { HRESULT hr = writablePropStore.Commit(); Marshal.ReleaseComObject(writablePropStore); writablePropStore = null; } ParentShellObject.NativePropertyStore = null; }
private void StorePropVariantValue(PropVariant propVar) { Guid guid = new Guid(ShellIIDGuid.IPropertyStore); IPropertyStore writablePropStore = null; try { int hr = ParentShellObject.NativeShellItem2.GetPropertyStore( ShellNativeMethods.GetPropertyStoreOptions.ReadWrite, ref guid, out writablePropStore); if (!CoreErrorHelper.Succeeded(hr)) { throw new Exception("Unable to get writable property store for this property.", Marshal.GetExceptionForHR(hr)); } HResult result = writablePropStore.SetValue(ref propertyKey, propVar); if (!AllowSetTruncatedValue && (int)result == ShellNativeMethods.InPlaceStringTruncated) { throw new ArgumentOutOfRangeException("propVar", "A value had to be truncated in a string or rounded if a numeric value. Set AllowTruncatedValue to true to prevent this exception."); } if (!CoreErrorHelper.Succeeded(result)) { throw new Exception("Unable to set property.", Marshal.GetExceptionForHR((int)result)); } writablePropStore.Commit(); } catch (InvalidComObjectException e) { throw new Exception("Unable to get writable property store for this property.", e); } catch (InvalidCastException) { throw new Exception("Unable to get writable property store for this property."); } finally { if (writablePropStore != null) { Marshal.ReleaseComObject(writablePropStore); writablePropStore = null; } } }
public void create(string shortcutPath = "") { if (!string.IsNullOrEmpty(shortcutPath)) { if (!shortcutPath.Contains("\\")) { this.ShortcutPath = this.generateLinkPath(shortcutPath); } else { this.ShortcutPath = shortcutPath; } } if (string.IsNullOrEmpty(this.ShortcutPath)) { this.ShortcutPath = this.generateDefaultLinkPath(); } string folderPath = Path.GetDirectoryName(this.ShortcutPath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } // Find the path to the current executable IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ResultChecker.VerifyAndThrow("SetPath", newShortcut.SetPath(this.ExePath)); ResultChecker.VerifyAndThrow("SetArguments", newShortcut.SetArguments(this.Arguments)); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; if (!string.IsNullOrEmpty(this.AppUserModelID)) { using (PropVariant appId = new PropVariant(this.AppUserModelID)) { ResultChecker.VerifyAndThrow("SetValue", newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ResultChecker.VerifyAndThrow("Commit", newShortcutProperties.Commit()); } } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ResultChecker.VerifyAndThrow("Save", newShortcutSave.Save(this.ShortcutPath, true)); }
/// <summary> /// Sets property value of the property /// </summary> /// <returns>Property value</returns> public void SetValue(PropertyKey key, object value) { ComThread.Assert(); if (Mode == AccessMode.Read) { return; } if (!Contains(key)) { return; } Marshal.ThrowExceptionForHR(_propertyStoreInteface.SetValue(ref key, ref value)); _propertyStoreInteface.Commit(); }
private static void SetProperty(IntPtr hwnd, PropertyKey key, PropVariant value) { IPropertyStore propStore = null; try { propStore = GetWindowPropertyStore(hwnd); propStore.SetValue(ref key, value); propStore.Commit(); } finally { if (propStore != null) { Marshal.ReleaseComObject(propStore); } } }
/// <summary> /// Change ribbon text color /// </summary> /// <param name="text">new text color</param> public void SetTextColor(Color text) { if (!Initialized) { return; } // convert color to proper color format uint color = ColorHelper.RGBToUInt32(text); IPropertyStore propertyStore = (IPropertyStore)Framework; PropVariant colorProp = PropVariant.FromObject(color); // set ribbon color propertyStore.SetValue(ref RibbonProperties.GlobalTextColor, ref colorProp); propertyStore.Commit(); }
private static void InstallShortcut(string shortcutPath, string APP_ID = "") { string _APP_ID = string.IsNullOrEmpty(APP_ID) ? pAPP_ID : APP_ID; string exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(_APP_ID + NotID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
/// <summary> /// 创建快捷方式 /// </summary> private void CreateShortcut(String shortcutPath, string appId) { String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant id = new PropVariant(appId)) { ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, id)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } IPersistFile newShortcutSave = (IPersistFile)newShortcut; ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
static void InstallShortcut(string sourcePath, string shortcutPath, string applicationId, string iconPath) { IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); ErrorHelper.VerifySucceeded(newShortcut.SetPath(sourcePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; if (iconPath != null) { newShortcut.SetIconLocation(iconPath, 0); } using (PropVariant appId = new PropVariant(applicationId)) { PropertyKey SystemProperties_System_AppUserModel_ID = new PropertyKey(new Guid("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}"), 5); ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties_System_AppUserModel_ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
/// <summary> /// Gets the shell <b>IShellLink</b> representation /// of the object. /// </summary> /// <returns>An <b>IShellLink</b> up-cast to <b>object</b>.</returns> public object GetShellRepresentation() { IShellLinkW shellLink = (IShellLinkW) new CShellLink(); IPropertyStore propertyStore = (IPropertyStore)shellLink; PropVariant propVariant = new PropVariant(); if (IsSeparator) { propVariant.SetValue(true); propertyStore.SetValue(ref PropertyKey.PKEY_AppUserModel_IsDestListSeparator, ref propVariant); propVariant.Clear(); } else { shellLink.SetPath(Path); if (!String.IsNullOrEmpty(IconLocation)) { shellLink.SetIconLocation(IconLocation, IconIndex); } if (!String.IsNullOrEmpty(Arguments)) { shellLink.SetArguments(Arguments); } if (!String.IsNullOrEmpty(WorkingDirectory)) { shellLink.SetWorkingDirectory(WorkingDirectory); } shellLink.SetShowCmd((uint)ShowCommand); propVariant.SetValue(Title); propertyStore.SetValue(ref PropertyKey.PKEY_Title, ref propVariant); propVariant.Clear(); } propertyStore.Commit(); return(shellLink); }
/// <summary> /// Creates a link file to the current executable and installs the shortcut at the path generated /// </summary> /// <param name="shortcutPath"> Shortcut path for installation of the shortcut</param> private static bool InstallShortcut(String shortcutPath) { Console.WriteLine("Installing a shortcut of the application on the start menu with path={0}", shortcutPath); try { // Finds the path to the current executable String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); Console.WriteLine("Current executable path={0}", exePath); // Create a shortcut to the executable ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); // Opens the shortcut property store and sets the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(APP_ID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commits the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); Console.WriteLine("Installation of the shortcut of the application on the start menu with path={0} completed", shortcutPath); return(true); } catch (Exception e) { Console.WriteLine("Insalling the shortcut failed due to Exception={0}", e.StackTrace); } return(false); }
private void install7Shortcut() { for (int i = 0; i < 2; i++) { try { IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ErrorHelper.VerifySucceeded(newShortcut.SetPath(InstallerData.MainProgramLocation)); ErrorHelper.VerifySucceeded(newShortcut.SetDescription(InstallerData.Description)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(GlobalAssemblyInfo.AssemblyName)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(InstallerData.ShortcutLocation, true)); break; } catch { if (i == 1) { throw; } } } }
internal static void SetWindowProperty(IntPtr hwnd, PropertyKey propkey, string value) { // Get the IPropertyStore for the given window handle IPropertyStore propStore = GetWindowPropertyStore(hwnd); //uint fucku; //propStore.GetCount(out fucku); //Console.Write("BEFORE PROPSTORE.SETVALUE::: " + fucku.ToString() + "\r\n"); // Set the value PropVariant pv = new PropVariant(); pv.SetString(value); propStore.SetValue(ref propkey, ref pv); propStore.Commit(); //propStore.GetCount(out fucku); //Console.Write("AFTER PROPSTORE.SETVALUE::: " + fucku.ToString() + "\r\n"); // Dispose the IPropertyStore and PropVariant Marshal.ReleaseComObject(propStore); pv.Clear(); }
/// <summary> /// 创建快捷方式 /// </summary> private static void CreatShortcut <T>(string shortcutPath) { String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(_aumid)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(new PropertyKey(TOAST_G, 5), appId)); } using (PropVariant toastid = new PropVariant(typeof(T).GUID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(new PropertyKey(TOAST_G, 26), toastid)); } ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
//In order to display toasts, a desktop application must have a shortcut on the Start menu. //The shortcut should be created as part of the installer. //Note: AppUserModelID must be set on that shortcut. public static void InstallShortcut() { try { string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\Start Menu\Programs\YtoX.lnk"; if (!File.Exists(shortcutPath)) { //get current executable path string exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); Log.Write("Shortcut to EXE: " + exePath); //Create a shortcut to the exe ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); //Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appIdProp = new PropVariant(Constants.APP_ID)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appIdProp)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } Log.Write("Shortcut filepath: " + shortcutPath); //Save the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); } } catch (Exception exception) { Log.Error(exception); } }
private static void InstallShortcut(String shortcutPath) { // Find the path to the current executable String exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments("")); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetIconLocation(System.IO.Directory.GetCurrentDirectory() + @"/logo.ico", 0)); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(APP_ID)) { ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
/// <summary> /// Creates a shortcut to enable the app to receive toast notifications. /// </summary> /// <remarks> /// Documentation: https://docs.microsoft.com/en-us/windows/win32/shell/enable-desktop-toast-with-appusermodelid /// </remarks> /// <param name="shortcutPath">Full path to the shortcut (including .lnk), must be in Program Files or Start Menu</param> /// <param name="appExecutablePath">Path the to app that receives notifications</param> /// <param name="arguments">Optional arguments</param> /// <param name="appName">The name of the app - used to create the toast</param> /// <param name="activatorId">The activation id</param> public static void RegisterAppForNotifications(string shortcutPath, string appExecutablePath, string arguments, string appName, string activatorId) { var shellLinkClass = new ShellLinkCoClass(); IShellLinkW shellLink = (IShellLinkW)shellLinkClass; shellLink.SetPath(appExecutablePath); IPropertyStore propertyStore = (IPropertyStore)shellLinkClass; IPersistFile persistFile = (IPersistFile)shellLinkClass; if (arguments != null) { shellLink.SetArguments(arguments); } // https://docs.microsoft.com/en-us/windows/win32/properties/props-system-appusermodel-id propertyStore.SetValue(new PropertyKey("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3", 5), new PROPVARIANT(appName)); // https://docs.microsoft.com/en-us/windows/win32/properties/props-system-appusermodel-toastactivatorclsid propertyStore.SetValue(new PropertyKey("9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3", 26), new PROPVARIANT(new Guid(activatorId))); propertyStore.Commit(); persistFile.Save(shortcutPath, true); }
private void InstallShortcut(string shortcutPath, string pAppId) { // Find the path to the current executable string exePath = Process.GetCurrentProcess().MainModule.FileName; IShellLinkW newShortcut = (IShellLinkW) new CShellLink(); // Create a shortcut to the exe ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath)); ErrorHelper.VerifySucceeded(newShortcut.SetArguments("this was a callback")); newShortcut.SetShowCmd(0); // Open the shortcut property store, set the AppUserModelId property IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut; using (PropVariant appId = new PropVariant(pAppId)) { ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId)); ErrorHelper.VerifySucceeded(newShortcutProperties.Commit()); } // Commit the shortcut to disk IPersistFile newShortcutSave = (IPersistFile)newShortcut; ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true)); }
private static IShellLinkW CreateLinkFromJumpTask(JumpTask jumpTask, bool allowSeparators) { if (string.IsNullOrEmpty(jumpTask.Title) && (!allowSeparators || !string.IsNullOrEmpty(jumpTask.CustomCategory))) { return(null); } IShellLinkW shellLinkW = (IShellLinkW)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("00021401-0000-0000-C000-000000000046"))); IShellLinkW result; try { string path = JumpList._FullName; if (!string.IsNullOrEmpty(jumpTask.ApplicationPath)) { path = jumpTask.ApplicationPath; } shellLinkW.SetPath(path); if (!string.IsNullOrEmpty(jumpTask.WorkingDirectory)) { shellLinkW.SetWorkingDirectory(jumpTask.WorkingDirectory); } if (!string.IsNullOrEmpty(jumpTask.Arguments)) { shellLinkW.SetArguments(jumpTask.Arguments); } if (jumpTask.IconResourceIndex != -1) { string pszIconPath = JumpList._FullName; if (!string.IsNullOrEmpty(jumpTask.IconResourcePath)) { if ((long)jumpTask.IconResourcePath.Length >= 260L) { return(null); } pszIconPath = jumpTask.IconResourcePath; } shellLinkW.SetIconLocation(pszIconPath, jumpTask.IconResourceIndex); } if (!string.IsNullOrEmpty(jumpTask.Description)) { shellLinkW.SetDescription(jumpTask.Description); } IPropertyStore propertyStore = (IPropertyStore)shellLinkW; using (PROPVARIANT propvariant = new PROPVARIANT()) { PKEY pkey = default(PKEY); if (!string.IsNullOrEmpty(jumpTask.Title)) { propvariant.SetValue(jumpTask.Title); pkey = PKEY.Title; } else { propvariant.SetValue(true); pkey = PKEY.AppUserModel_IsDestListSeparator; } propertyStore.SetValue(ref pkey, propvariant); } propertyStore.Commit(); IShellLinkW shellLinkW2 = shellLinkW; shellLinkW = null; result = shellLinkW2; } catch (Exception) { result = null; } finally { Utility.SafeRelease <IShellLinkW>(ref shellLinkW); } return(result); }