示例#1
0
 /// <summary>Registers the file association.</summary>
 /// <param name="ext">The ext.</param>
 /// <param name="progId">The ProgID.</param>
 /// <param name="perceivedType">Type of the perceived.</param>
 /// <param name="contentType">Type of the content.</param>
 /// <param name="systemWide">if set to <c>true</c> register system wide.</param>
 /// <exception cref="ArgumentNullException">ext or progId</exception>
 /// <exception cref="ArgumentException">Extension must start with a '.' - ext or Undefined ProgId value. - progId</exception>
 /// <exception cref="InvalidOperationException">Unable to create association key in the registry.</exception>
 public static void RegisterFileAssociation(string ext, string progId, PERCEIVED perceivedType = PERCEIVED.PERCEIVED_TYPE_UNSPECIFIED, string contentType = null, bool systemWide = false)
 {
     if (ext == null)
     {
         throw new ArgumentNullException(nameof(ext));
     }
     if (!ext.StartsWith("."))
     {
         throw new ArgumentException("Extension must start with a '.'", nameof(ext));
     }
     if (progId == null)
     {
         throw new ArgumentNullException(nameof(progId));
     }
     if (!IsDefined(progId))
     {
         throw new ArgumentException("Undefined ProgId value.", nameof(progId));
     }
     using (var pkey = GetRoot(systemWide, true).CreateSubKey(ext))
     {
         if (pkey == null)
         {
             throw new InvalidOperationException("Unable to create association key in the registry.");
         }
         pkey.SetValue(null, progId);
         if (perceivedType > 0)
         {
             pkey.SetValue("PerceivedType", perceivedType.ToString().Substring(15).ToLower());
         }
         if (!string.IsNullOrEmpty(contentType))
         {
             pkey.SetValue("Content Type", contentType);
         }
     }
     NotifyShell();
 }
示例#2
0
 public static extern HRESULT AssocGetPerceivedType([MarshalAs(UnmanagedType.LPWStr)] string pszExt, ref PERCEIVED ptype, ref PERCEIVEDFLAG pflag, [MarshalAs(UnmanagedType.LPWStr)] ref StringBuilder ppszType);