I have a class library that is nested two+ layers under a main GUI application, within that nested class library I want to be able to access the main applications name.
Under .Net 3.5 you could call Application.ProductName to retrieve the value from the Assembly.cs file, but I cannot identify an equivalent in WPF. If I use reflection and GetExecutingAssembly then it returns the class libraries details?
Thanks
You can use Assembly.GetEntryAssembly() to get the EXE assembly, and can then use Reflection to get the AssemblyProductAttribute from that.
This assumes that the product name has been set on the EXE assembly. The WinForms Application.ProductName property actually looked in the assembly containing the main form, so it works even if the GUI is built in a DLL. To replicate this in WPF you would use Application.Current.MainWindow.GetType().Assembly (and again use Reflection to get the attribute).
Here is another solution that I am using to get the Product Name
Public Shared Function ProductName() As String
If Windows.Application.ResourceAssembly Is Nothing Then
Return Nothing
End If
Return Windows.Application.ResourceAssembly.GetName().Name
End Sub
in wpf there are many way to do this ,
here you can find two of this.
using System;`
using System.Windows;
String applicationName = String.Empty;
//one way
applicationName = AppDomain.CurrentDomain.FriendlyName.Split('.')[0];
//other way
applicationName = Application.ResourceAssembly.GetName().Name;
If you need to get the descriptive product name as I did, then this solution may be useful:
// Get the Product Name from the Assembly information
string productName = String.Empty;
var list = Application.Current.MainWindow.GetType().Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true);
if (list != null)
{
if (list.Length > 0)
{
productName = (list[0] as AssemblyProductAttribute).Product;
}
}
It returns whatever you've set for the 'AssemblyProduct' attribute in the AssemblyInfo.cs file, e.g. something like "Widget Engine Professional".
Based on the answers above, this works just great immediately:
var productName = Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyProductAttribute))
.OfType<AssemblyProductAttribute>()
.FirstOrDefault().Product;
If you are looking for the values provided by the assembly information, e.g. the title...
... then you have to get the custom attributes like this:
using System.Linq;
using System.Reflection;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Title = (Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute)).SingleOrDefault() as AssemblyTitleAttribute)?.Title;
}
}
}
The answer you require is:
Path.GetFileName(Assembly.GetEntryAssembly().GetName().Name)
Related
In the DotNetYaml sample code I'm looking at, there's a C# construct:
var deserializer = new Deserializer(namingConvention: new CamelCaseNamingConvention());
var order = deserializer.Deserialize<Order>(input);
What is the equivalent F# code? I've tried
let deserializer = new Deserializer(namingConvention=new CamelCaseNamingConvention())
deserializer.Deserialize<Meta>(input)
If you have a C# library that defines optional parameters, then you can use the syntax you are using in your question. To quickly show that's the case, I compiled the following C# code as a library:
using System;
namespace Demo {
public class MyClass {
public static void Foo(int first, string second = "foo", string third = "bar") { }
}
}
You can reference this and use it from F# as follows:
open Demo
MyClass.Foo(1, third="hi")
I tried to do this with YamlDotNet which, I guess, is the library that you were using, but I get an error that the Deserializer class does not have namingConvention as an argument, so my guess would be that you are probably using a different version of the library than you are thinking (or perhaps, my guess of what library you're using was wrong...).
Here is my Enums.cs file that exists in a CPSLibrary Class Library:
namespace CPSLibrary.CPSEnums
{
public enum GoalType
{
STRATEGIC = 1,
TACTICAL = 2
}
}
In a code behind file within a web application that references CPSLibrary, I'm doing the following:
using CPSLibrary;
/* ... farther down the page ... */
proj.Goal == CPSLibrary.CPSEnums.GoalType.STRATEGIC;
That will work, but if I try to just reference it like CPSEnums.GoalType.STRATEGIC it won't. Additionally, if I add "using CPSLibary.CPSEnums" I can then reference it simply as GoalType.STRATEGIC.
What do I need to do to get this to recognize CPSEnums.GoalType.STRATEGIC ?
Oddly enough, other classes with the CPSLibrary Class Library can reference it as CPSEnums.GoalType.STRATEGIC just fine.
Bonus Question: in this example, does "CPSEnums" have a technical term? "Container" or something like that? Or is it just a part of the Namespace with no separate terminology?
TIA
Try changing your using statement to this...
using CPSEnums = CPSLibrary.CPSEnums;
This should allow you to reference it the way you want...
/* ... farther down the page ... */
proj.Goal == CPSEnums.GoalType.STRATEGIC;
Because your namespace name is CPSLibrary.CPSEnums, so you can even write like:
using CPSLibrary.CPSEnums;
....
proj.Goal == GoalType.STRATEGIC; //NO NAMESPACE NAME
when you write using CPSLibrary, you refer to the "parent" namespace of your defined one. This is perfectly valid. But to access your enum type, you need specify its namepsace, and its namespace is: CPSLibrary.CPSEnums
Try this:
namespace CPSLibrary
{
public static class CPSEnums
{
public enum GoalType
{
STRATEGIC = 1,
TACTICAL = 2
}
}
}
var x = CPSEnums.GoalType.STRATEGIC;
I am using the following code under ASP.NET 4.0 framework to obtain the version of MSI file from a web app:
string strVersion = "";
try
{
Type InstallerType;
WindowsInstaller.Installer installer;
InstallerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
installer = (WindowsInstaller.Installer)Activator.CreateInstance(InstallerType);
WindowsInstaller.Database db = installer.OpenDatabase(strMSIFilePath, 0);
WindowsInstaller.View dv = db.OpenView("SELECT `Value` FROM `Property` WHERE `Property`='ProductVersion'");
WindowsInstaller.Record record = null;
dv.Execute(record);
record = dv.Fetch();
strVersion = record.get_StringData(1).ToString();
dv.Close();
//db.Commit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(dv);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(db);
}
catch
{
//Failed
strVersion = "";
}
It works fine except that when the code finishes running it holds an internal MSI file handle so when I try to move or rename the MSI file I get the error that the file is still in use. This continues until I actually navigate away from the ASPX page that calls the method above.
My question is, I obviously didn't close some handle or object in the code above. But what could that be?
PS. I'm testing it in a development IDE from VS2010.
EDIT: Edited the code like it should be after Adriano's suggestion. Thanks!
The COM object has not been released (it should be auto-released when it goes out of scope but in .NET this doesn't work really well). Because it does not implement the IDisposable interface you can't call its Dispose() method and you can't use it inside an using statement. You have to explicitly call Marshal.FinalReleaseComObject. For example:
try
{
// Your stuffs
}
finally
{
dv.Close();
Marshal.FinalReleaseComObject(dv);
Marshal.FinalReleaseComObject(db);
}
Moreover note that you do not really need a call to the Commit() method because you didn't make any change but just a query.
FWIW, you should be using Windows Installer XML (WiX) Deployment Tools Foundation (DTF). It's an FOSS project from Microsoft that can be found on CodePlex. It has MSI interop libraries with classes that are very similar to the COM classes but implement IDisosable and use P/Invoke instead of COM behind the scenes. There is even support for Linq to MSI if you want. And the full source code is available.
DTF is the gold standard for MSI interop in a .NET world. Here are two examples:
using System;
using System.Linq;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Deployment.WindowsInstaller.Linq;
namespace ConsoleApplication3
{
class Program
{
const string DATABASE_PATH = #"C:\FOO..MSI";
const string SQL_SELECT_PRODUCTVERSION = "SELECT `Value` FROM `Property` WHERE `Property`='ProductVersion'";
static void Main(string[] args)
{
using (Database database = new Database(DATABASE_PATH, DatabaseOpenMode.ReadOnly))
{
Console.WriteLine(database.ExecuteScalar(SQL_SELECT_PRODUCTVERSION).ToString());
}
using (QDatabase database = new QDatabase(DATABASE_PATH, DatabaseOpenMode.ReadOnly))
{
var results = from property in database.Properties where property.Property == "ProductVersion" select property.Value;
Console.WriteLine(results.AsEnumerable<string>().First());
}
}
}
}
try to Dispose the Objects.
dv.Dispose();
db.Dispose();
I just don't know how to explain this clearly. So I create a simple image pattern of what I did.
My question is, how would I be able to access my database in other class in LS?
I've been searching on net, but I didn't found any solution. I hope I'll find it here.
Thanks!.
Any suggestion is already appreciated.
Thanks for the answer Bryan, but I found the answer on my problem here Richard Waddell
Here is what I did to achieve my goal.
Switch your LS project to file view
Go to "Common" project, under "UserCode" folder, create a class (e.g. Authenticate.cs) and put this codes.
The code follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
public class Authenticate
{
public static adminuser GetCurrentUser()
{
adminuser userFound = (from useritem in
Application.Current.CreateDataWorkspace().basecampcoreData.adminusers
where useritem.LoginID == Application.Current.User.Name
select useritem).SingleOrDefault();
if (userFound != null)
return userFound;
else
return null;
}
}
}
Then you can now call the Authenticate.GetCurrentUser() anywhere in the project.
Thanks!
The main difference is the first set of code that works is running inside a screen. For your Authenticate class, you need to do the following steps to access the Database.
Note: I'm assuming that your datasource has the default name of ApplicationData since you hid the name, if not, make the corresponding changes. If it's a completely different datasource, change "_IntrinsicData" in the steps below)
These steps are taken from the Lightswitch Help Website
Navigate to ..ServerGenerated\GeneratedArtifacts (in the LightSwitch project) and click on ApplicationData.cs and Add As Link.
Add the following code below, this code dynamically creates a connection to the database. LightSwitch uses “_IntrinsicData” as it’s connection string.
private ApplicationDataObjectContext m_context;
public ApplicationDataObjectContext Context
{
get
{
if (this.m_context == null)
{
string connString =
System.Web.Configuration.WebConfigurationManager
.ConnectionStrings["_IntrinsicData"].ConnectionString;
EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
builder.Metadata =
"res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
builder.Provider =
"System.Data.SqlClient";
builder.ProviderConnectionString = connString;
this.m_context = new ApplicationDataObjectContext(builder.ConnectionString);
}
return this.m_context;
}
}
You should be able to access it through Context.adminusers
How can I add a namespace to a c# project? I am a beginner.
if(!string.IsNullOrEmpty(result))
{
CoderBuddy.ExtractEmails helper = new CoderBuddy.ExtractEmails(result);
EmailsList = helper.Extract_Emails;
}
My Form1 needs to use the namespace below:
// this is the file that I need to add
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Coderbuddy
{
public class ExtractEmails
{
private string s;
public ExtractEmails(string Text2Scrape)
{
this.s = Text2Scrape;
}
public string[] Extract_Emails()
{
string[] Email_List = new string[0];
Regex r = new Regex(#"[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}", RegexOptions.IgnoreCase);
Match m;
//Searching for the text that matches the above regular expression(which only matches email addresses)
for (m = r.Match(s); m.Success; m = m.NextMatch())
{
//This section here demonstartes Dynamic arrays
if (m.Value.Length > 0)
{
//Resize the array Email_List by incrementing it by 1, to save the next result
Array.Resize(ref Email_List, Email_List.Length + 1);
Email_List[Email_List.Length - 1] = m.Value;
}
}
return Email_List;
}
}
}
Well, add a using statement in your .cs page
using Coderbuddy;
Then your code can access the methods exposed by this type.
OR, put your winform .cs file in the same namespace (not a recommended idea)
Put this at the top of your code-behind file:
using Coderbuddy;
Read this introduction to namespaces and assemblies on MSDN.
(I am assuming you need to add that second file to your own project. If it is already part of another project in your solution, then add it as a project reference as Darkhydro has answered.)
You don't need to explicitly add namespaces to your project. The namespace declaration in line 6 of the file that you need to use does it implicity.
For this example, add a blank file called ExtractEmails.cs to your project (the convention if a file contains only one class definition is to name the file after the class), and then paste that code into it. Boom - namespace added :)
In your form code, you are already using the fully qualified name of the class (that is, you are mentioning the namespace in the line
CoderBuddy.ExtractEmails helper = new CoderBuddy.ExtractEmails(result);
so you don't need a "using" statement.
If you did add "using CoderBuddy;" to the top of your form's .cs file, then that line could change to
ExtractEmails helper = new ExtractEmails(result);
But in this case I would leave it as you already have it, because the namespace hints at the fact that the ExtractEmails code is slightly separated from the rest of your code.