public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string s) { return(IndirectString.TryParse(s, out var loc) ? loc : null); } return(base.ConvertFrom(context, culture, value)); }
/// <summary>Tries to parse the specified string to create a <see cref="IndirectString"/> instance.</summary> /// <param name="value">The string representation in the format of either "ModuleFileName,ResourceIndex" or "ModuleFileName,-ResourceID".</param> /// <param name="loc">The resulting <see cref="IndirectString"/> instance on success.</param> /// <returns><c>true</c> if successfully parsed.</returns> public static bool TryParse(string value, out IndirectString loc) { var parts = value?.Split(','); if (parts != null && parts.Length == 2 && int.TryParse(parts[1], out var i) && parts[0].StartsWith("@")) { loc = new IndirectString(parts[0].TrimStart('@'), i); return(true); } loc = new IndirectString(value); return(!string.IsNullOrEmpty(value)); }
/// <summary>Tries to parse the specified string to create a <see cref="IndirectString"/> instance.</summary> /// <param name="value">The string representation in the format of either "ModuleFileName,ResourceIndex" or "ModuleFileName,-ResourceID".</param> /// <param name="loc">The resulting <see cref="IndirectString"/> instance on success.</param> /// <returns><c>true</c> if successfully parsed.</returns> public static bool TryParse(string value, out IndirectString loc) { loc = new IndirectString(value); return(loc.IsValid || value != null); }
public static void RegisterApplication(string fullExePath, bool userOnly = false, bool acceptsUrls = false, Guid?dropTarget = null, IndirectString friendlyName = null, IEnumerable <string> supportedTypes = null, IconLocation defaultIcon = null, bool noStartPage = false, IconLocation taskGroupIcon = null, bool useExecutableForTaskbarGroupIcon = false) { if (fullExePath == null) { throw new ArgumentNullException(nameof(fullExePath)); } fullExePath = Path.GetFullPath(fullExePath); var fn = Path.GetFileName(fullExePath).ToLower(); // Handle registrations in user or machine "App Paths" var root = userOnly ? Registry.CurrentUser : Registry.LocalMachine; using (var reg = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths")) using (var sk = reg?.CreateSubKey(fn)) { if (sk == null) { throw new InvalidOperationException("Unable to create application key in the 'App Paths' subkey."); } // Build short path and store as default value var shortPath = fullExePath; var l = fullExePath.Length + 5; var sb = new StringBuilder(l, l); var rl = PInvoke.Kernel32.GetShortPathName(fullExePath.Length > PInvoke.Kernel32.MAX_PATH ? @"\\?\" + fullExePath : fullExePath, sb, (uint)sb.Capacity); if (rl > 0 && rl <= l) { shortPath = sb.ToString(); } sk.SetValue(null, shortPath); // Add Path value sk.SetValue("Path", Path.GetDirectoryName(fullExePath)); // Add UseUrl value if needed if (acceptsUrls) { sk.SetValue("UseUrl", 1U, RegistryValueKind.DWord); } // Add DropTarget GUID if needed if (dropTarget != null) { sk.SetValue("DropTarget", dropTarget.Value.ToString()); } } // Handle registrations in HKCR\Applications using (var reg = Registry.ClassesRoot.OpenSubKey(@"Applications")) using (var sk = reg?.CreateSubKey(fn)) { if (sk == null) { throw new InvalidOperationException("Unable to create application key in the HKCR\\Applications subkey."); } if (friendlyName != null) { sk.SetValue("FriendlyAppName", friendlyName.ToString()); } if (supportedTypes != null) { using (var stk = sk.CreateSubKey("SupportedTypes")) foreach (var s in supportedTypes) { stk?.SetValue(s, string.Empty, RegistryValueKind.String); } } if (defaultIcon != null) { sk.CreateSubKey("DefaultIcon", defaultIcon.ToString()); } if (noStartPage) { sk.SetValue("NoStartPage", string.Empty, RegistryValueKind.String); } if (taskGroupIcon != null) { sk.SetValue("TaskbarGroupIcon", taskGroupIcon.ToString()); } if (useExecutableForTaskbarGroupIcon) { sk.SetValue("UseExecutableForTaskbarGroupIcon", string.Empty, RegistryValueKind.String); } } NotifyShell(); }