Tutorial C# : Work Samples / PDF
Tutorial C# : Work Samples / PDF
Function ProcessFolder .................
Introduction
This
document provides a number of examples of C# code that I designed and developed.
See
Notes and Caveats at the end of this document.
Example(s):
Description
This document contains 1 example:
Example 1. This example is part of an
application (desktop application) to extract email addresses from Microsoft
Outlook 2007 folders. It uses automation to interact with Outlook.
Example(s):
Code
Example
1:
//****************************************
// Program: WEED - (Windows) Extract
Email Details
// Program type: Windows application
(.exe)
// Version: 0.2
// Date: 26 Feb 2009
//----------------------------------------
// Author: Andrew Fry
// Copyright: Copyright (c) 2009 A.R.Fry.
All rights reserved.
//****************************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Office;
using Microsoft.Office.Core;
using Outlook =
Microsoft.Office.Interop.Outlook;
namespace ExtractEmailDetails
{
public partial class Form1 : Form
{
// Array of addresses...
public ArrayList Addrs1 = new ArrayList();
// Array of ignore strings...
public string[] aIgnores;
public String Ignores;
// Flags...
public bool SkipJunk;
public bool CheckSubFolders;
public bool StatusGo;
public Form1()
{
InitializeComponent();
this.RBTargO.Checked = true;
this.RBTargOE.Checked = false;
this.RBTargOE.Enabled = false;
this.RBFoldA.Checked = true;
this.RBFoldS.Checked = false;
this.CBIncSF.Checked = true;
this.CBSort.Checked = false;
this.TBInF.Text =
"C:\\Program Files\\Weed\\Ignore.txt";
this.TBOutF.Text = "C:\\Program
Files\\Weed\\EmailAddresses.txt";
}
//****************************************
// Function: ButGo_Click
// Purpose: Handles Go button, on click
//****************************************
private void ButGo_Click(object sender, EventArgs e)
{
Addrs1.Clear();
SkipJunk = true;
CheckSubFolders = true;
if (!this.CBIncSF.Checked)
{
CheckSubFolders = false;
}
StatusGo = true;
// Get addresses...
GetEmailAddresses();
this.TBStat.Text =
"Done!";
Application.Exit();
}
//****************************************
// Function: ButCancel_Click
// Purpose: Handles Cancel button, on click
//****************************************
private void ButCancel_Click(object sender, EventArgs e)
{
StatusGo = false;
}
//****************************************
// Function: ButExit_Click
// Purpose: Handles Exit button, on click
//****************************************
private void ButExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//****************************************
// Function: GetEmailAddresses
//****************************************
private void GetEmailAddresses()
{
Outlook.ApplicationClass oOLApp =
null;
Outlook.NameSpace oOLNS = null;
Outlook.Folders oOLFolders =
null;
Outlook.MAPIFolder oOLFolder;
// Top level folder
string InFileIgn =
this.TBInF.Text;
StreamReader fIn;
string OutFile =
this.TBOutF.Text;
StreamWriter fOut;
bool DoSort =
this.CBSort.Checked;
int N = 0;
FileInfo fi;
char[] newline =
"\n".ToCharArray();
oOLApp = new Outlook.ApplicationClass();
// Setup Namespace
oOLNS =
oOLApp.GetNamespace("MAPI");
if
(!string.IsNullOrEmpty(InFileIgn))
{
if (File.Exists(InFileIgn))
{
fi = new FileInfo(InFileIgn);
if (fi.Length > 0)
{
this.TBStat.Text =
"Reading input file...";
Application.DoEvents();
fIn = new
StreamReader(InFileIgn);
//fIn =
File.OpenText(InFileIgn);
Ignores =
fIn.ReadToEnd();
fIn.Close();
aIgnores =
Ignores.Split(newline);
}
}
}
// All folders or specific
folder ?
if (this.RBFoldA.Checked)
{
oOLFolders = oOLNS.Folders;
foreach (Outlook.MAPIFolder
oOLFolder1 in oOLFolders)
{
//FolderName =
oOLFolder.Name
//Addrs1.Add("Folder: " + FolderName);
//Addrs1.Add("DIT:
" + CStr(oOLFolderL0.DefaultItemType));
ProcessFolder(oOLFolder1);
}
}
else
{
oOLFolder =
oOLNS.PickFolder();
ProcessFolder(oOLFolder);
}
oOLApp = null;
if (StatusGo)
{
if (DoSort)
{
this.TBStat.Text =
"Sorting results...";
Application.DoEvents();
Addrs1.Sort();
}
// Write results to output
file...
fOut =
File.CreateText(OutFile);
for (N = 0; N <
Addrs1.Count; N++)
{
fOut.WriteLine(Addrs1[N].ToString());
}
fOut.Close();
}
}
//****************************************
// Function: ProcessFolder
// Purpose: ...
// Returns: Array of addresses
//****************************************
private void ProcessFolder(Outlook.MAPIFolder StartFolder)
{
string FolderName = null;
string FolderPath = null;
bool bProceed = true;
FolderName = StartFolder.Name;
FolderPath =
StartFolder.FolderPath;
if (StatusGo)
{
this.TBStat.Text =
FolderPath;
Application.DoEvents();
if (SkipJunk == true &
(string.Equals(FolderName, "Junk E-mail") |
string.Equals(FolderName, "Spam")))
{
bProceed = false;
}
}
else
{
bProceed = false;
}
if (bProceed)
{
// process all the items in
this folder...
if (StartFolder.Items.Count
> 0)
{
foreach (object oItem in
StartFolder.Items)
{
Application.DoEvents();
if (!StatusGo) break;
//ItemName =
oItem.FullName
//Addrs1.Add("Item: " + ItemName);
// Is it a
mail item ?
if (oItem is
Outlook.MailItem)
{
GetAddrs1((Outlook.MailItem)oItem);
this.TBStat.Text
= FolderPath + " (" + Addrs1.Count + ")";
Application.DoEvents();
}
}
}
// process all the subfolders
in this folder...
if (CheckSubFolders)
{
if
(StartFolder.Folders.Count > 0)
{
foreach
(Outlook.MAPIFolder oFolder in StartFolder.Folders)
{
//FolderName =
objFolder.Name
//Addrs1.Add("Folder:
" + oFolder.FolderPath);
//Addrs1.Add("Folder: " + FolderName);
//Addrs1.Add("DIT: " + CStr(objFolder.DefaultItemType));
ProcessFolder(oFolder);
}
}
}
}
}
(NB. Remainder of code not shown)
|
Explanation
Example 1.
The
example shows 5 functions.
Functions
ButGo_Click, ButCancel_Click and ButExit_Click are event handlers and are tied
to the ‘Go’, ‘Cancel’ and ‘Exit’ buttons respectively. ButGo_Click – which is
invoked when the user presses the ‘Go’ button - performs some initialization
and then calls GetEmailAddresses.
Function
GetEmailAddresses is the main function. It performs the following steps:
§ It starts Outlook (if not
already running).
§ It opens and reads the
input file (which contains a set of strings to be ignored).
§ It determines the starting
folder – by checking whether the user has chosen all folders or a selected
folder – and then calls ProcessFolder in order to process the current folder
(and subfolders). This process builds an array of email addresses (into
Addrs1()).
§ It then sorts the results.
§ It then writes the results
to the output file.
Tutorial C# : Work Samples / PDF
0 commentaires: