C# Tips and Tricks
Reading Outlook Contacts
Have you ever realized all you can do if Outlook contacts information were located into a database? If you have, this tip is for you. The first step is to take all that information out from Outlook so we can transform it into series of insert/update statements.
Start Microsoft Visual C# Express (you can download it for free from Microsoft’s site pending link).
Once downloaded and installed, create a new project and add Outlook’s library to it:
Go to Project -> Add Reference -> COM and select Microsoft Outlook 11.0 Object Library
Add the following line to the beginning of the file:
using Outlook;
Update:
Last time I did it I had to change last line to this:
using Outlook = Microsoft.Office.Interop.Outlook;
And here’s the code:
Outlook.Application app = new ApplicationClass();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
ns.Logon("outlook-username", "outlook-password", false, true);
MAPIFolder apf;
MAPIFolder contacts;
string storeId;
apf = ns.GetDefaultFolder(
OlDefaultFolders.olPublicFoldersAllPublicFolders);
contacts = apf.Folders["your-public-folder-name"];
storeId = contacts.StoreID;
foreach (Object o in contacts.Items){
// Casting is important to avoid any distribution list
if (o is ContactItem)
{
ContactItem c = (ContactItem)o;
Console.Writeline(c.LastName + "n");
}
}
ns.Logoff();
Ok, we now know how to access and loop contacts, the missing thing is how to save them in an external database.
By the way, the ns.Logon function receives four parameters:
- string Profile: that’s your username,
- string Password: that’s your username,
- bool ShowDialog: that’s your username and
- bool NewSession: that’s your username
If the contact list you’re trying to access is not in the root level, but is a subfolder inside a subfolder inside a subfolder… like in the following image:
then this is the way you should go:
apf = ns.GetDefaultFolder(
OlDefaultFolders.olPublicFoldersAllPublicFolders);
contacts =
apf.Folders["International"].Folders["MX"].Folders["Admin Contacts"];
Sending mail from C#
An easy way to send email using C#:
System.Net.Mail.MailMessage message =
new System.Net.Mail.MailMessage();
message.To.Add("luckyperson@online.microsoft.com");
message.Subject = "This is the Subject line";
message.From =
new System.Net.Mail.MailAddress("from@online.microsoft.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp =
new System.Net.Mail.SmtpClient("yoursmtphost");
smtp.Send(message);
source: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=717&SiteID=1
System.Transactions
If you want to use System.Transactions in your application,
- Right click project name in Solution Explorer and click Add a Reference…
- In the .NET tab find and select the System.Transactions option
- Click Ok and enjoy.
If you don’t add the reference you will get the following error while compiling:
The type or namespace name ‘Transactions’ does not exist in the namespace ‘System’ (are you missing an assembly reference?)
Creating a message box
Well, it is very simple…
MessageBox.Show("Your message")
There are many available options… be sure you take a look at them.
Getting the Windows user name, domain, etc…
There are two ways to accomplish this, first the short way:
Environment.UserName // user
Environment.UserDomainName // domain
And the long way:
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
The long way will give you access to many more options so be sure to explore it.
How to read a text file
Add the following code at the beginning of your file:
Using System.IO;
And here’s the code…
String text;
try {
StreamReader sr = new StreamReader("C:file.txt");
text = sr.ReadLine();
while (text != null) {
Console.WriteLine(text);
text = sr.ReadLine();
}
sr.Close();
Console.ReadLine();
}
catch(Exception ex) {
Console.WriteLine("Exception: " + ex.Message);
}
How to write a text file
Add the following code at the beginning of your file:
Using System.IO;
And here it goes…
try {
StreamWriter sw = new StreamWriter("C:file.txt");
sw.WriteLine("Hello World!!!");
sw.WriteLine("This is a test");
sw.Close();
}
catch(Exception e) {
Console.WriteLine("Exception: " + e.Message);
}
How to connect to Access
Code:
using System.Data.OleDb;
...
OleDbConnection conn = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:database.mdb;" +
"Jet OLEDB:Database Password=password");
conn.Open();
OleDbCommand cmd = new OleDbCommand("select * from mytable", conn);
OleDbDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(dr.GetString(0));
}
conn.Close();
How to connect to MySQL
See my MySQL section.
Error: IErrorInfo.GetDescription Failed with E_FAIL(0x80004005)
When you are querying a database you get this error. The reason is your query is using a keyword for a column name, table, etc. It does not matter if your query works well when you execute it directly in your database manager, remember there are more layers between C# and your database.
Data Types for C# and MSAccess 2002
If you want to do a “perfect” match between data types in MSAccess and C#
C# data types
<th>
CLR
</th>
<th>
Signed
</th>
<th>
Memory
</th>
<th>
Range
</th>
<td>
System.Sbyte
</td>
<td>
Yes
</td>
<td>
1 byte
</td>
<td>
€“128 to 127
</td>
<td>
System.Byte
</td>
<td>
No
</td>
<td>
1 byte
</td>
<td>
0 to 255
</td>
<td>
System.Int16
</td>
<td>
Yes
</td>
<td>
2 bytes
</td>
<td>
32768 to 32767
</td>
<td>
System.Int32
</td>
<td>
Yes
</td>
<td>
4 bytes
</td>
<td>
2147483648 to 2147483647
</td>
<td>
System.Int64
</td>
<td>
Yes
</td>
<td>
8 bytes
</td>
<td>
9223372036854775808 to 9223372036854775807
</td>
<td>
System.Uint16
</td>
<td>
No
</td>
<td>
2 bytes
</td>
<td>
0 to 65535
</td>
<td>
System.Uint32
</td>
<td>
No
</td>
<td>
4 bytes
</td>
<td>
0 to 4294967295
</td>
<td>
System.Uint64
</td>
<td>
No
</td>
<td>
8 bytes
</td>
<td>
0 to 18446744073709551615
</td>
<td>
System.Single
</td>
<td>
Yes
</td>
<td>
4 bytes
</td>
<td>
1.5×10-45 to 3.4 x x1038
</td>
<td>
System.Double
</td>
<td>
Yes
</td>
<td>
8 bytes
</td>
<td>
5.0×10-324 to 1.7×10308
</td>
<td>
System.Decimal
</td>
<td>
Yes
</td>
<td>
12 bytes
</td>
<td>
1.0×10-28 to 7.9×1028
</td>
<td>
System.Char
</td>
<td>
</td>
<td>
2 bytes
</td>
<td>
Unicode characters
</td>
<td>
System.Boolean
</td>
<td>
</td>
<td>
1 byte
</td>
<td>
True or false
</td>
MS Access data types
<th>
Range
</th>
<th>
Decimal precision
</th>
<th>
Memory
</th>
<td>
0 to 255 (no fractions)
</td>
<td>
None
</td>
<td>
1 byte
</td>
<td>
10^38-1 through 10^38-1 (.adp)<br /> -10^28-1 through 10^28-1 (.mdb)
</td>
<td>
28
</td>
<td>
12bytes
</td>
<td>
-32768 to 32767 (no fractions).
</td>
<td>
None
</td>
<td>
2 bytes
</td>
<td>
(Default) -2147483648 to 2147483647 (no fractions).
</td>
<td>
None
</td>
<td>
4 bytes
</td>
<td>
-3.402823E38 to -1.401298E-45 for negative values and<br /> 1.401298E-45 to 3.402823E38 for positive values.
</td>
<td>
7
</td>
<td>
4 bytes
</td>
<td>
-1.79769313486231E308 to<br /> -4.94065645841247E€“324 for negative values and<br /> 4.94065645841247E-324 to<br /> 1.79769313486231E308 for positive values.
</td>
<td>
15
</td>
<td>
8 bytes
</td>
<td>
Globally unique identifier (GUID)
</td>
<td>
N/A
</td>
<td>
16 bytes
</td>