how to show publish version in a textbox? - c#

At the moment I am manually updating the version field (textbox) in my application every time I publish it. I am wondering if there is a way to have my application get that data from somewhere and display it in the box for me. I am using VS2012 and I am just unsure of how to achieve that in C#. Below is a screenshot of the VS2012 properties window that I am talking about.
NEW IMAGE:

Don't forget to check if the application is networkdeployed otherwise it won't work in debug mode.
if (ApplicationDeployment.IsNetworkDeployed)
{
this.Text = string.Format("Your application name - v{0}",
ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString(4));
}

Try this:
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
Then the caller to the getter property can de-reference the Major, Minor, Build and Revision properties, like this:
YourVersionTextBox.Text = AssemblyVersion.Major.ToString() + "."
+ AssemblyVersion.Minor.ToString() + "."
+ AssemblyVersion.Build.ToString() + "."
+ AssemblyVersion.Revision.ToString();

Also we can use overloadedToString of System.Version
using System.Deployment.Application;
public Version AssemblyVersion
{
get
{
return ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
}
YourVersionTextBox.Text = AssemblyVersion.ToString(1); // 1 - get only major
YourVersionTextBox.Text = AssemblyVersion.ToString(2); // 1.0 - get only major, minor
YourVersionTextBox.Text = AssemblyVersion.ToString(3); // 1.0.3 - get only major, minor, build
YourVersionTextBox.Text = AssemblyVersion.ToString(4); // 1.0.3.4 - get only major, minor, build, revision

Method 1 :
You can use this
string version = Application.ProductVersion;
and show the version in your textBox.
Method 2 :
or if you want the version parts separately, you can use this :
System.Version version2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
now you have these :
version2.Major;
version2.Minor;
version2.Revision;
version2.Build;
and you can use them like this
string versionString= (String.Format("{0}.{1}.{2}.{3}", version2.Major, version2.Minor, version2.Revision, version2.Build));

If you get an error of ApplicationDeployment, then you can try this:
For global version accessing through Program, declare inside Program class:
private static Version version = new Version(Application.ProductVersion);
public static Version Version
{
get
{
return version;
}
}
Now you have Program.Version anywhere available in the program and you can use it to get version information like this:
LabelVersion.Text = String.Format("Version {0}.{1}",
Program.Version.Major.ToString(),
Program.Version.Minor.ToString());

Related

Visual Studio Debugger Extension get user settings

I'm writing a visual studio extension based on the Concord Samples Hello World project. The goal is to let the user filter out stack frames by setting a list of search strings. If any of the search strings are in a stack frame, it is omitted.
I've got the filter working for a hardcoded list. That needs to be in a non-package-based dll project in order for the debugger to pick it up. And I have a vsix project that references that dll with an OptionPageGrid to accept the list of strings. But I can't for the life of me find a way to connect them.
On the debugger side, my code looks something like this:
DkmStackWalkFrame[] IDkmCallStackFilter.FilterNextFrame(DkmStackContext stackContext, DkmStackWalkFrame input)
{
if (input == null) // null input frame indicates the end of the call stack. This sample does nothing on end-of-stack.
return null;
if (input.InstructionAddress == null) // error case
return new[] { input };
DkmWorkList workList = DkmWorkList.Create(null);
DkmLanguage language = input.Process.EngineSettings.GetLanguage(new DkmCompilerId());
DkmInspectionContext inspection = DkmInspectionContext.Create(stackContext.InspectionSession, input.RuntimeInstance, input.Thread, 1000,
DkmEvaluationFlags.None, DkmFuncEvalFlags.None, 10, language, null);
string frameName = "";
inspection.GetFrameName(workList, input, DkmVariableInfoFlags.None, result => GotFrameName(result, out frameName));
workList.Execute();
CallstackCollapserDataItem dataItem = CallstackCollapserDataItem.GetInstance(stackContext);
bool omitFrame = false;
foreach (string filterString in dataItem.FilterStrings)
{
if (frameName.Contains(filterString))
{
omitFrame = true;
}
}
The CallstackCollapserDataItem is where I theoretically need to retrieve the strings from user settings. But I don't have access to any services/packages in order to e.g. ask for WritableSettingsStore, like in You've Been Haacked's Example. Nor can I get my OptionPageGrid, like in the MSDN Options Example.
The other thing I tried was based on this StackOverflow question. I overrode the LoadSettingsFromStorage function of my OptionPageGrid and attempted to set a static variable on a public class in the dll project. But if that code existed in the LoadSettingsFromStorage function at all, the settings failed to load without even entering the function. Which felt like voodoo to me. Comment out the line that sets the variable, the breakpoint hits normally, the settings load normally. Restore it, and the function isn't even entered.
I'm at a loss. I really just want to pass a string into my Concord extension, and I really don't care how.
Ok, apparently all I needed to do was post the question here for me to figure out the last little pieces. In my CallstackCollapserDataItem : DkmDataItem class, I added the following code:
private CallstackCollapserDataItem()
{
string registryRoot = DkmGlobalSettings.RegistryRoot;
string propertyPath = "vsix\\CallstackCollapserOptionPageGrid";
string fullKey = "HKEY_CURRENT_USER\\" + registryRoot + "\\ApplicationPrivateSettings\\" + propertyPath;
string savedStringSetting = (string)Registry.GetValue(fullKey, "SearchStrings", "");
string semicolonSeparatedStrings = "";
// The setting resembles "1*System String*Foo;Bar"
if (savedStringSetting != null && savedStringSetting.Length > 0 && savedStringSetting.Split('*').Length == 3)
{
semicolonSeparatedStrings = savedStringSetting.Split('*')[2];
}
}
vsix is the assembly in which CallstackCollapserOptionPageGrid is a DialogPage, and SearchStrings is its public property that's saved out of the options menu.

Iterate through settings files

I'm currently working on a VSTO project for which I have 5 .settings files:
Settings.settings (Default)
s201213.settings
s201314.settings
s201415.settings
s201516.settings
Over time there will be more settings files included following the same naming convention ('s' followed by a tax year).
I know I can iterate through a settings file, but is there a way to iterate through the actual settings files themselves?
I've tried things such as:
public void Example()
{
System.Collections.IEnumerator testSetting = MyAddIn.Properties.s201213.Default.Properties.GetEnumerator();
while (testSetting.MoveNext())
{
System.Diagnostics.Debug.WriteLine("Setting:\t" + testSetting.Current.ToString());
}
}
Which obviously only iterates through a single settings file, but I can't seem to figure out the logic of iterating through all the settings files as a collection, without explicitly naming each one in the code. I hope that makes sense, any help is appreciated.
Update:
I think I'm getting somewhere with the following code:
foreach(Type test in Assembly.GetExecutingAssembly().GetTypes())
{
if (System.Text.RegularExpressions.Regex.IsMatch(test.Name, "^s[0-9]{6}$"))
{
PropertyInfo value = test.GetProperty("LEL");
try
{
System.Diagnostics.Debug.WriteLine("Name:\t" + test.Name +
"\nNameSpace:\t" + test.Namespace +
"\nProperty:\t" + test.GetProperty("LEL").ToString() +
"\n");
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
}
}
Which seems to be recognising the settings files and the stored values:
Output:
Name: s201213
NameSpace: MyAddIn.Properties
Property: Double LEL
Name: s201314
NameSpace: MyAddIn.Properties
Property: Double LEL
Name: s201415
NameSpace: MyAddIn.Properties
Property: Double LEL
Name: s201516
NameSpace: MyAddIn.Properties
Property: Double LEL
However I can't seem to get the actual value of the "LEL" setting which should return a Double?
2nd Update
I've actually given up and decided to use a local DB instead - but I would still like to know if this is possible, and I think other people would like to know too so I'm going to throw a bounty at it to try and generate some interest.
Jeremy's answer got me to the finish post, but thought I'd post the final code I used so that it can be seen in context:
public void GetLEL()
{
var fileMap = new ConfigurationFileMap(AppDomain.CurrentDomain.BaseDirectory + #"CustomAddIn.dll.config");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("userSettings");
var section = (ClientSettingsSection)sectionGroup.Sections.Get("MyAddIn.s201213");
var setting = section.Settings.Get("LEL");
System.Diagnostics.Debug.WriteLine(setting.Value.ValueXml.InnerXml);
// Prints "107" as expected.
}
The answer is really simple once you see it (no need to iterate through Types nor use System.IO directory):
using System.Configuration; //Add a reference to this DLL
...
var fileMap = new ConfigurationFileMap(Application.StartupPath + #"\GetSettingFilesValues.exe.config");
var configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
var sectionGroup = configuration.GetSectionGroup("applicationSettings"); // This is the section group name, change to your needs, ie application or user
var section = (ClientSettingsSection)sectionGroup.Sections.Get("GetSettingFilesValues.Properties.s201415"); //This is the section name, change to your needs (you know the tax years you need)
var setting = section.Settings.Get("LEL");
System.Diagnostics.Debug.WriteLine(setting.Value.ValueXml.InnerXml);
I agree with your approach to use the dB, though I'm sure this will benefit other people too.
I believe you can use the System.IO namespace classes for iterating through files with the same extension. There is no built-in mechanisms or properties for that.

Passing Guid to Command Line parser Library in C#

I am using the nuget package Command Line Parser for parsing command line arguments in C#.
How do I pass a GUID from command line?
ApplicationName.exe -g="3a0e5412-0971-4e0e-aebc-29dd09907b31"
does not work.
My CommandLineArgs class is
[Option('g', "sampleguid", Required = true, HelpText = "Enter a sample GUID")]
public Guid MyGuid { get; set; }
First, there is no built-in functionality for Guid. Second, make sure you're using the latest version 2.6.0.5. You can install it through nuget -> search for "CommandLineArgumentsParser".
Once you've installed the latest version, you can interpret custom structures as such:
var parser = new CommandLineParser.CommandLineParser();
var guidArgument = new ValueArgument<Guid>('g', "guid", "Guid of something");
guidArgument.ConvertValueHandler = Guid.Parse;
parser.Arguments.Add(guidArgument);
parser.ParseCommandLine(args);
// the actual guid from command line.
var parsedGuid = guidArgument.Value;
If you want to keep your current version, you need to treat Guid as string when parsing, and later do custom validation by yourself.
http://commandlineparser.codeplex.com/wikipage?title=More%20thorough%20examples&referringTitle=Home
As you can see from GUID is not working for input parameter, the Guid parsing issue was already fixed. (However, if you use Guid?, it still does not work.)
Have you tried dropping the "=" and using a " " (space) instead?
Like: ApplicationName.exe -g "3a0e5412-0971-4e0e-aebc-29dd09907b31"

Xamarin iOS localization using .NET

I'm trying to use .NET localization in a portable class library for a Xamarin iOS/Android project. I've followed the steps at:
How to use localization in C#
And have code which looks as follows:
string sText = strings.enter_movie_name;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr");
sText = strings.enter_movie_name;
lblEnterMovieName.Text = sText;
I've also tried:
ResourceManager resman = new ResourceManager(typeof(MyApplication.strings));
string sText = resman.GetString("enter_movie_name", new CultureInfo("fr"));
I've created strings.resx with enter_movie_name equal to "Enter movie name:" and strings.fr.resx equal to "Entre la movie name:"
However, sText always ends up as "Enter movie name:". I can't seem to get the "Entre la movie name:" version out.
I also saw the post at Cross-platform Localization but couldn't work it out. I also don't want to use the iOS specific version at Localization on Xamarin.iOS as I'd like to be able to get at my strings from a platform independent library.
Can anyone point out where I'm going wrong?
I've created a bit of an ugly solution, but it works. What I've done is made a folder called 'strings' in my Portable Class Library (PCL) and inside that created files called:
strings.resx
strings_fr.resx
strings_ja_JP.resx
I've set all of these as Embedded Resource with custom tool as ResXFileCodeGenerator. This means I have a separate resource DLL for each language.
In iOS I can then get the locale by calling:
string sLocale = NSLocale.CurrentLocale.LocaleIdentifier;
I would guess there's an Android equivalent using Locale but I don't know what it is.
This gives me a string like "ja_JP" or "fr_FR" or "en_GB" (note they're underscores, not dashes). I then use this with the following static class I created to retrieve an appropriate resource manager and get the string from it.
If given a locale aa_BB it first looks for strings_aa_BB, then for strings_aa, then for strings.
public static class Localise
{
private const string STRINGS_ROOT = "MyPCL.strings.strings";
public static string GetString(string sID, string sLocale)
{
string sResource = STRINGS_ROOT + "_" + sLocale;
Type type = Type.GetType(sResource);
if (type == null)
{
if (sLocale.Length > 2) {
sResource = STRINGS_ROOT + "_" + sLocale.Substring(0, 2); // Use first two letters of region code
type = Type.GetType(sResource);
}
}
if (type == null) {
sResource = STRINGS_ROOT;
type = Type.GetType(sResource);
if (type == null)
{
System.Diagnostics.Debug.WriteLine("No strings resource file when looking for " + sID + " in " + sLocale);
return null; // This shouldn't ever happen in theory
}
}
ResourceManager resman = new ResourceManager(type);
return resman.GetString(sID);
}
}
An example of how this would be used (referring to the above code) would be:
string sText = Localise.GetString("enter_movie_name", sLocale);
lblEnterMovieName.Text = sText;
A significant downside of this is that all views will need to have their text set programatically, but does have the upside that the translations can be done once and then reused on many platforms. They also remain separate from the main code in their own DLLs and therefore can be recompiled individually if necessary.
I created a similar solution to the accepted answer but using txt files instead of Resx and nuget ready to go: https://github.com/xleon/I18N-Portable. Blog post here.
Other improvements are:
"anyKey".Translate(); // from anywhere
Automatic detection of the current culture
Get a list of supported translations: List<PortableLanguage> languages = I18N.Current.Languages;
Support for Data Binding in Mvvm frameworks / Xamarin.Forms
etc

Determining the version of an MSI without installing it

I have an MSI file built from my C# Visual Studio 2010. The version is set through the Version property. I wanted to know if there is a way to determine the version without having to install the file. Currently when right click and view the properties it isn't displayed.
The following code may be helpful. But remember that you should first add a COM reference to the Microsoft Windows Installer Object Library and add the WindowsInstaller namespace to your code. The following function may be what you need.
public static string GetMsiInfo( string msiPath, string Info)
{
string retVal = string.Empty;
Type classType = Type.GetTypeFromProgID( “WindowsInstaller.Installer” );
Object installerObj = Activator.CreateInstance( classType );
Installer installer = installerObj as Installer;
// Open msi file
Database db = installer.OpenDatabase( msiPath, 0 );
// Fetch the property
string sql = String.Format(“SELECT Value FROM Property WHERE Property=’{0}’”, Info);
View view = db.OpenView( sql );
view.Execute( null );
// Read in the record
Record rec = view.Fetch();
if ( rec != null )
retVal = rec.get_StringData( 1 );
return retVal;
}
If you need the version, pass in the name of the MSI file you want, e.g.
string version = GetMsiInfo( "d:\product.msi", “ProductVersion” );
Yes - I think you need to inspect the MSI database however, which requires either some API calls or a wrapper utility.
Microsofts ORCA application should let you do this (although I've never tried it myself).
Instead of using the COM library, you can use the Microsoft.Deployment.WindowsInstaller library from the wixtoolset SDK. Once referenced, you can very similarly get the version info.
private string GetMsiInfo(string msiPath)
{
using (var database = new Microsoft.Deployment.WindowsInstaller.Database(msiPath))
{
var sql = "SELECT Value FROM Property WHERE Property ='ProductVersion'";
using (var view = database.OpenView(sql))
{
view.Execute();
using (var record = view.Fetch())
{
var version = record?.GetString(1);
return version;
}
}
}
}
I haven't found a way to get the correct assembly via nuget installer. However, after I installed the wixtoolset https://wixtoolset.org/releases/, I was able to add a reference in my project directly under assemblies -> extensions -> Microsoft.Deployment.WindowsInstaller.
Based on Gupta's answer, I added COM release calls. If you want to recreate or replace the file you accessed in your further workflow, it might be still in use and you will get an exception if the GC did not yet release the objects, so let's do this manually.
public static string GetMsiInfo(string msiPath, string info)
{
string retVal = string.Empty;
Type classType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
dynamic installer = Activator.CreateInstance(classType);
try
{
// Open msi file
var db = installer.OpenDatabase(msiPath, 0);
try
{
// Fetch the property
string sql = $"SELECT Value FROM Property WHERE Property ='{info}'";
var view = db.OpenView(sql);
try
{
view.Execute(null);
// Read in the record
var rec = view.Fetch();
if (rec != null)
retVal = rec.StringData(1);
return retVal;
}
finally
{
view.Close();
Marshal.ReleaseComObject(view);
}
}
finally
{
//db.Commit();
Marshal.ReleaseComObject(db);
}
}
finally
{
Marshal.ReleaseComObject(installer);
}
}
I think using this code, there is no need to add a COM reference or an extra namespace as mentioned by Gupta, because we use late binding here (see the dynamic).

Categories