c# How to get the version of This current executable .exe - c#

I have been using this code to obtain my programs version:
public string progVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
However, it doesn't always seem to grab the version I'm expecting. I don't really understand how this works entirely or what it's doing.
I think it is impart because I'm launching my program from another program then it appears to grab the version of the program that launched it instead, or the 'GetExecutingAssembly()' I'm assuming references the program that executed my program, like so:
System.Diagnostics.Process.Start(my_programs_path);
Is there a more reliable way to get the program version of the actual program at the time I ask for it?
Perhaps even launch my program without leaving some kind of trail, as if the user themselves just launched it.
Thanks for any help!

GetExecutingAssembly() returns the assembly that contains the method that calls it. If you call it from a library you will always get the version of the library not the application executable.
To get the application's executable use GetEntryAssembly()
Consider the following example:
In AssemblyA:
class VersionTest
{
void Test()
{
Console.Write("Executing assembly: {0}\n", Assembly.GetExecutingAssembly().GetName().ToString()); // returns AssemblyA
Console.Write("Entry assembly: {0}\n", Assembly.GetEntryAssembly().GetName().ToString()); // returns AssemblyB
}
}
In AssemblyB:
class Program
{
void Main()
{
var ver = new VersionTest();
ver.Test();
}
}

You could use the Assembly property of a known type via typeof which is defined in your application to ensure you got the 'correct' assembly and then retrieve the version of that, e.g.
typeof(YourKnownType).Assembly.GetName().Version.ToString();

Thanks for the great info. I finally settled on
Assembly.GetExecutingAssembly().GetName().Version.ToString()
for my solution.

Related

Retrieve <Version> tag written in csproj file from dll [duplicate]

I am trying to get the executing assembly version in C# 3.0 using the following code:
var assemblyFullName = Assembly.GetExecutingAssembly().FullName;
var version = assemblyFullName .Split(',')[1].Split('=')[1];
Is there another proper way of doing so?
Two options... regardless of application type you can always invoke:
Assembly.GetExecutingAssembly().GetName().Version
If a Windows Forms application, you can always access via application if looking specifically for product version.
Application.ProductVersion
Using GetExecutingAssembly for an assembly reference is not always an option. As such, I personally find it useful to create a static helper class in projects where I may need to reference the underlying assembly or assembly version:
// A sample assembly reference class that would exist in the `Core` project.
public static class CoreAssembly
{
public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
public static readonly Version Version = Reference.GetName().Version;
}
Then I can cleanly reference CoreAssembly.Version in my code as required.
In MSDN, Assembly.GetExecutingAssembly Method, is remark about method "getexecutingassembly", that for performance reasons, you should call this method only when you do not know at design time what assembly is currently executing.
The recommended way to retrieve an Assembly object that represents the current assembly is to use the Type.Assembly property of a type found in the assembly.
The following example illustrates:
using System;
using System.Reflection;
public class Example
{
public static void Main()
{
Console.WriteLine("The version of the currently executing assembly is: {0}",
typeof(Example).Assembly.GetName().Version);
}
}
/* This example produces output similar to the following:
The version of the currently executing assembly is: 1.1.0.0
Of course this is very similar to the answer with helper class "public static class CoreAssembly", but, if you know at least one type of executing assembly, it isn't mandatory to create a helper class, and it saves your time.
using System.Reflection;
{
string version = Assembly.GetEntryAssembly().GetName().Version.ToString();
}
Remarks from MSDN http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly%28v=vs.110%29.aspx:
The GetEntryAssembly method can return null when a managed assembly has been loaded from an unmanaged application. For example, if an unmanaged application creates an instance of a COM component written in C#, a call to the GetEntryAssembly method from the C# component returns null, because the entry point for the process was unmanaged code rather than a managed assembly.
Product Version may be preferred if you're using versioning via GitVersion or other versioning software.
To get this from within your class library you can call System.Diagnostics.FileVersionInfo.ProductVersion:
using System.Diagnostics;
using System.Reflection;
//...
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var productVersion = FileVersionInfo.GetVersionInfo(assemblyLocation).ProductVersion
This should do:
Assembly assem = Assembly.GetExecutingAssembly();
AssemblyName aName = assem.GetName();
return aName.Version.ToString();
I finally settled on typeof(MyClass).GetTypeInfo().Assembly.GetName().Version for a netstandard1.6 app. All of the other proposed answers presented a partial solution. This is the only thing that got me exactly what I needed.
Sourced from a combination of places:
https://msdn.microsoft.com/en-us/library/x4cw969y(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/2exyydhb(v=vs.110).aspx

sharing a static class with a DLL in C# without passing a reference

VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. I've managed to break my code, and can't understand what I've done wrong. (i didn't think i had changed anything, but ...)
I have a solution which contains 2 projects. The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project.
the first project contains a form, and a static class with public static strings in the same namespace. (and some other unconnected classes). specifically:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
when the form is closing, the method shown above is called which calls the second projects class project2_class constructor.
in project 2, the DLL, there is:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating:
error CS0103: The name 'settings' does not exist in the current context
the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL.
I really honestly don't think i had changed anything relevant to this, but also clearly I have.
looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons).
would really appreciate help with this, as its been a lot of work to get this working.
as a side note, I've definitely learned my lesson about not using source control. and not making "how this works" comments instead of "what this does" comments.
may dynamic help you? You can not get the setting string at complie time.

LuaInterface - How can I load an assembly from any path?

I am trying to load a .net assembly using LuaInterface. If I place the assembly in the same folder as my executable (and my LuaInterface.dll and LuaNet.dll) then everything works great. I would like to move the assembly into a different folder, but when I try that I get "A .NET exception occured in user-code". I have tried:
package.path = package.path .. "C:\\path\\to\\my\\assembly\\?.dll"
luanet.load_assembly("MyAssembly")
and
luanet.load_assembly("C:\\path\\to\\my\\assembly\\MyAssembly")
and
luanet.load_assembly("C:\\path\\to\\my\\assembly\\MyAssembly.dll")
All of these return the .NET exception error. Is there a way to define the path that LuaInterface uses?
Your assembly is loaded by your "hosting" executable, and not really loaded by the Lua environment itself. luanet.load_assembly("MyAssembly") simply makes the assembly accessible to the Lua environment. For example (C#):
using MyAssembly; //you can't compile unless MyAssembly is available
namespace LuaRunner
{
class LuaRunner
{
void DoLua()
{
using (LuaInterface.Lua lua = new LuaInterface.Lua())
{
lua.DoString("luanet.load_assembly('MyAssembly')");
//... do what you want within Lua with MyAssembly
}
}
}
}
Your running program is the "host" for Lua to run within, so it's your running program that actually loads MyAssembly. Your executable needs a reference to MyAssembly.dll, (and needs to be able to find it at runtime in the usual locations).
To search other assemblies, set the package.cpath variable. For example:
package.cpath = DATA_DIR .. "\\clibs\\?.dll;" .. package.cpath
From the Lua 5.1 documentation:
require (modname)
First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath.
package.cpath
The path used by require to search for a C loader.
Lua initializes the C path package.cpath in the same way it initializes the Lua path package.path, using the environment variable LUA_CPATH or a default path defined in luaconf.h.

ResourceManager.GetString fails in unit tests

I've created an assembly MyResources with two resx:
MyResources.resx
MyResources.en.resx
Inside the assembly I've added a handler-class containing a GetString-wrapper inside a ResHandler-class:
public string GetResString(string key)
{
return _manager.GetString(key, _culture);
}
_culture is simply a property which can be set from outside:
public void ChangeCulture(CultureInfo newCulture)
{
_culture = newCulture;
}
If I call this code from a lets say console-app, everything works fine:
var res = ResHandler.GetInstance(Guid.NewGuid().ToString());
//change the culture to "en"
res.ChangeCulture(new CultureInfo("en"));
Console.WriteLine(res.GetResString("TXT_0001"));
This code writes the english version to the console. However, if I call the exact same code from a unit-test-method, the contents of the MyResources.resx will appear. Whats wrong here? Are unit-tests unable to do this for some reason?
Beware that satellite assemblies are stored in a subdirectory of the directory that contains the EXE. Like "en-US" or "en" for English. Problem is, your test runs under a different EXE, mstest.exe and not your app.exe. It will therefore not find the satellite assembly. I think you can fix this by using Deployment in the test settings, not sure.

What does "Unhandled Exception: GLib.GException: Unhandled tag: 'requires'" mean?

I'm trying to get my linux Gtk# application working on Windows. When I try to run it, I get this error message:
Unhandled Exception: GLib.GException:
Unhandled tag: 'requires'
at Gtk.Builder.AddFromFile(String
filename)
at Interface.MainWindow..ctor()
at [My Project Name].MainClass.Main(String[]
args) in c:\Path\To\Main.cs:line 10
It seems to be happening when trying to build the interface from my Glade file. I've checked and the path to the glade file is correct. What might be going wrong?
Here is some code to reproduce the problem:
using System;
using Gtk;
namespace TestGtk {
class MainClass {
public static void Main (string[] args)
{
Application.Init();
string gladefile = #"C:\path\to\gladefile.glade";
Builder builder = new Builder();
builder.AddFromFile(gladefile);
Application.Run();
}
}
}
Strange... I don't know why on windows GTK# does not support requires. Anyway I'd try to remove the <requires ... /> tag from gladefile.glade.
This most likely means that your Glade file is corrupt or has got some weirdness in it.
You're using GtkBuilder to load GladeXML files. GtkBuilder has different XML format, incompatible with GladeXML (it more generic). If you use glade-3 to design your UI, you have an option to save as GtkBuilder XML or GladeXML. Also, glade has utility called gtk-builder-convert that you can use to convert GladeXML to GtkBuilder XML.
So, there are two options:
Use glade-3 and save your UI in GtkBuilder format
Use gtk-builder-convert utility
Glade is for GTK 3.x and your system is probably on GTK 4.x.
I had a similar issue when the version was not specified in a Python app using a .glade file, and upon running it would show:
Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded.
It worked prior to an Ubuntu update last I ran if. After adding
import gi
gi.require_version("Gtk", "3.0")
It works.
A similar issue was noted in a Haskell app. I am not sure how one changes the reference to GTK3 on C#.

Categories

Resources