Wednesday, February 21, 2007

C# .Net 2.0 Get MAC Address with WMI

Problem:

Need to get the MAC address from workstation where
software is installed.


Solution:

.Net 2.0
C#

Add Reference: System.Management


private string GetMacAddress()
{
//ManagementObjectSearcher namespace is System.Management
//add .Net reference

ManagementObjectSearcher query = null;
ManagementObjectCollection queryCollection = null;

try
{
query = new
ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");

queryCollection = query.Get();


foreach (ManagementObject mo in queryCollection)
{
if ((bool)mo["IPEnabled"] == true)
{
//System.Diagnostics.Debug.WriteLine(mo.GetText(TextFormat.Mof));
string mac_address = mo["MacAddress"].ToString();

mo.Dispose();
queryCollection.Dispose();
query.Dispose();

return (mac_address);
}
}

return (string.Empty);

}
catch (System.Exception er)
{
System.Diagnostics.Debug.WriteLine(er.Message.ToString());
return (string.Empty);
}
}

Wednesday, February 7, 2007

C# 2.0 Web.Config read AppSettings ConfigurationManager

Problem:
How to read AppSetting in Web.Config file

<appSettings>
<add key="IsMaximized" value="True"/>
<add key="IsUsed" value="False"/>
</appSettings>

How do you get the value for the key = IsUsed?

Answer
There are three ways.

1) string sGet = ConfigurationManager.AppSettings.Get("IsUsed");

2) string sIndexer = ConfigurationManager.AppSettings["IsUsed"].ToString();

3)
using System.Collections.Specialized;

NameValueCollection nvc = ConfigurationManager.AppSettings;
string sNVC = nvc["IsUsed"];

C# Boxing and UnBoxing

Certification Test Question:

Give an example that demostrates unboxing?


Answer:
Value type to object (boxing)
object back to value type (unboxing)

Example:
object o = 123;
int one2three = (int)o;

----------
object pi = 3.14159265
double dPI = (double)pi;

unboxing is object to ValueType

MSDN:
Unboxing extracts the value type from the object.

Thursday, February 1, 2007

C# Function : Date to Julian

problem:
Function to convert Date into Julian format


solution:

private string Date2Julian(DateTime dt)
{
//julianDate YYYYnnn
// YYYY = 4 digit year
// nnn = number of days since january 1 of YYYY

// Jan 1, 2005 = 2005001
// Dec 31, 2005 = 2005365

//initialize variable
string sJulianDate=string.Empty;


sJulianDate = dt.Year.ToString()+
dt.DayOfYear.ToString().PadLeft(3,'0');

return (sJulianDate);
}

VBA :: ADODB Function to Connect to Access

Problem:
Need function to connect to an Access Database.

--make sure set a reference to ADO object in order to use
ADODB.Connection object

Solution:

Function cnConnectToAccessDB(ByVal sFileName As String) As ADODB.Connection
Dim CN As New ADODB.Connection
Dim sCS As String

'you must have ado 2.x installed on machine
' x = 5|6|7

'open this file in Exclusive mode
'comment this line out if you don't want exclusive connection
CN.Mode = adModeShareExclusive


'build connection string
sCS = "Provider=Microsoft.Jet.OLEDB.4.0;"
sCS = sCS & "Data Source=" & sFileName & ";"

'open connection
CN.Open sCS

'return to calling function
'adodb is an object, so you have to SET
Set cnConnectToAccessDB = CN
End Function