crisoft Posted June 5, 2008 at 04:12 PM Report Share #189653 Posted June 5, 2008 at 04:12 PM Olá a todos. Comecei à pouco tempo a programar em C#, por isso sou uma novata nisto e ainda estou na fase de explorar as funcionalidades para poder criar a minha primeira desktop application. Alguém me sabe dizer como passar automáticamente para uma caption a versão da aplicação? Para visualizar numa janela do tipo About? Link to comment Share on other sites More sharing options...
skm Posted June 6, 2008 at 08:55 AM Report Share #189732 Posted June 6, 2008 at 08:55 AM Depende da aplicação. Se for uma aplicação web podes colocar uma key no ficheiro de config e ler esse key. lblVersion.Text = ConfigurationManager.AppSettings["SITE_VERSION"]; Ou podes usar o reflection para ler a versão que colocaste na assembly do teu programa. using System.Reflection; public static void AssemblyProperties(ref string product, ref string version) { try { Assembly objAssembly = Assembly.GetExecutingAssembly(); foreach(Attribute attr in Attribute.GetCustomAttributes(objAssembly)) { if (attr.GetType() == typeof(AssemblyProductAttribute)) { product = ((AssemblyProductAttribute)attr).Product; } if (attr.GetType() == typeof(AssemblyVersionAttribute)) { version = ((AssemblyVersionAttribute)attr).Version; } } string delimStr = ",="; char[] delimiter = delimStr.ToCharArray(); string[] strAssembly = objAssembly.ToString().Split(delimiter); version = strAssembly[2]; } catch(Exception ex) { Log(ex); } } "There are two kinds of programmers. Those who write something to get the work done and those who want to write good code."João BrandãoWebsite e blog: http://jamab.blogspot.com/ Link to comment Share on other sites More sharing options...
crisoft Posted June 6, 2008 at 10:35 AM Author Report Share #189748 Posted June 6, 2008 at 10:35 AM Muito obrigado. Entretanto também descobri que que se inserir um novo item na minha aplicação desktop do tip About Box, posso adaptar a parit do código gerado toda a informação de que preciso: using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Reflection; using System.Windows.Forms; namespace PPTime { partial class AboutBox1 : Form { public AboutBox1() { InitializeComponent(); this.Text = String.Format("About {0} {0}", AssemblyTitle); this.labelProductName.Text = AssemblyProduct; this.labelVersion.Text = String.Format("Version {0} {0}", AssemblyVersion); this.labelCopyright.Text = AssemblyCopyright; this.labelCompanyName.Text = AssemblyCompany; this.textBoxDescription.Text = AssemblyDescription; } #region Assembly Attribute Accessors public string AssemblyTitle { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; if (titleAttribute.Title != "") { return titleAttribute.Title; } } return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); } } public string AssemblyVersion { get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); } } public string AssemblyDescription { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyDescriptionAttribute)attributes[0]).Description; } } public string AssemblyProduct { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyProductAttribute)attributes[0]).Product; } } public string AssemblyCopyright { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; } } public string AssemblyCompany { get { object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false); if (attributes.Length == 0) { return ""; } return ((AssemblyCompanyAttribute)attributes[0]).Company; } } #endregion } } Mas não há dúvidas que o teu método resume exactamente o que eu precisava... Muito obrigada, mais uma vez crisoft Link to comment Share on other sites More sharing options...
crisoft Posted June 6, 2008 at 10:37 AM Author Report Share #189749 Posted June 6, 2008 at 10:37 AM Já agora alguém pode-me explicar qual a melhor forma de passar código para aqui (para ficar com uma formatação adequada?) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now