最近,做了几个winform小程序,需要获得winform程序集信息内容。
以下为主要代码
获取标题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 标题 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyTitle { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">object</span>[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(AssemblyTitleAttribute), <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (attributes.Length > <span style="color: #800080;">0</span><span style="color: #000000;">) { AssemblyTitleAttribute titleAttribute </span>= (AssemblyTitleAttribute)attributes[<span style="color: #800080;">0</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span> (titleAttribute.Title != <span style="color: #800000;">""</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> titleAttribute.Title; } } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); } }</span> |
获取版本号
1 2 3 4 5 6 7 8 9 10 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 版本号 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyVersion { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> Assembly.GetExecutingAssembly().GetName().Version.ToString(); } }</span> |
获取描述内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 描述 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyDescription { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">object</span>[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(AssemblyDescriptionAttribute), <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (attributes.Length == <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> ((AssemblyDescriptionAttribute)attributes[<span style="color: #800080;">0</span><span style="color: #000000;">]).Description; } }</span> |
获取产品
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 产品 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyProduct { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">object</span>[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(AssemblyProductAttribute), <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (attributes.Length == <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> ((AssemblyProductAttribute)attributes[<span style="color: #800080;">0</span><span style="color: #000000;">]).Product; } }</span> |
获取版权信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 版权 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyCopyright { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">object</span>[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(AssemblyCopyrightAttribute), <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (attributes.Length == <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> ((AssemblyCopyrightAttribute)attributes[<span style="color: #800080;">0</span><span style="color: #000000;">]).Copyright; } }</span> |
获取公司
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 公司 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> AssemblyCompany { </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">object</span>[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(<span style="color: #0000ff;">typeof</span>(AssemblyCompanyAttribute), <span style="color: #0000ff;">false</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span> (attributes.Length == <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> ((AssemblyCompanyAttribute)attributes[<span style="color: #800080;">0</span><span style="color: #000000;">]).Company; } }</span> |