Editing text in body of email via Outlook add-in - c#

The following is an add-in for MS Outlook. One can scan the body of an e-mail and if there is a certain (specific word or pattern) word present, then a MessageBox appears. However, I am wondering if it is possible to change the way a word appears or to edit the text in the body of email, without MessageBox whatsoever. For example, a word (such as the name of the company) can be converted into an hyperlink (i.e. Google to www.google.com or Microsoft to www.microsoft.com) and the user who reads emails on Outlook always sees the hyperlink instead of the word itself.
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Drawing;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace FirstOutlookAddIn
{
public partial class ThisAddIn
{
public static string[] data = new string[10];
public static Stopwatch timer = new Stopwatch();
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
timer = Stopwatch.StartNew(); ReadMail();
outlookNameSpace = this.Application.GetNamespace("MAPI");
inbox = outlookNameSpace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.
OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(ReadSingleMail); // Modified method to run for single email
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Hinweis: Outlook löst dieses Ereignis nicht mehr aus. Wenn Code vorhanden ist, der
// ausgeführt werden muss, wenn Outlook geschlossen wird, informieren Sie sich unter http://go.microsoft.com/fwlink/?LinkId=506785
}
static void ReadSingleMail(dynamic item)
{
string bodyText; // Email body
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Path to My Documents
if (item != null)
{
bodyText = item.Body;
}
else
{
return; // If no e-mail body, exit function.
}
}
static void ReadMail()
{
//Set up OCR
string bodyText;
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//Get unread emails from Inbox
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items unreadItems = inboxFolder.Items.Restrict("[Unread]=true");
int max_runs;
//Go through each Unread email
if (unreadItems.Count > 10) { max_runs = 10; }
else max_runs = unreadItems.Count;
for (int counter = 1; counter <= max_runs; counter++)
{
//Reinitialize Data array
for (int index = 0; index <= 8; index++)
{
data[index] = "";
}
dynamic item = unreadItems[counter];
bodyText = item.Body;
Match match = Regex.Match(bodyText, "Insert searched pattern here");
if (match.Success)
{
MessageBox.Show(match.Value);
match = match.NextMatch();
}
}
}
#region Von VSTO generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}

Creating the function ReadMail() as below replaces text, as I want it.
static void ReadMail(){
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items Items = inboxFolder.Items.Restrict("[LastModificationTime] > '01/1/2003'");
foreach (var item in Items){
var mail = (Outlook.MailItem)item;
mail.Body = mail.Body.Replace("Text to be replaced", "Replacing text");
mail.Save();
}
}

Related

saving an encrypted piece of text and then opening an decrypting it using a GUI application

i have built an application where I can input a word or message into a textbox and click on my cipher button this will then generate a random key and encrypt that message I can then decipher that message by clicking my decipher button.MY problem is with my new features of a save and open button with this I want to save a message in its RAW enciphered format then be able to open it and decipher it as well as any other messages that have already been enciphered and previously saved. my problem is that this is not working I think it might be a problem with my save button because I also need to save the key as well as the message to decipher the message correctly this is just one of the things I have tried and I am quite stuck with this problem any help would be appreciated.Also the cipher I am using is the caesar cipher
the image is the GUI of the first stage of my working application with the message uptown funk being encryption. And obviously below is the code.
Edit-I have tried creating a tuple class to solve my problem but I have only used a class once never mind a tuple class and to put it bluntly after staring at it for a few hours I do not know how to get this to work some assistance would be appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
namespace login_form
{
public partial class Main : Form
{
public string cipherText;
public string originalText;
public string DecipherText;
public int key;
public Main()
{
InitializeComponent();
}
public static char cipher(char ch, int key)
{
if (!char.IsLetter(ch))
{
return ch;
}
char d = char.IsUpper(ch) ? 'A' : 'a';
return (char)((((ch + key) - d) % 26) + d);
}
public static string Encipher(string input, int key)
{
string output = string.Empty;
foreach (char ch in input)
output += cipher(ch, key);
return output;
}
public static string Decipher(string input, int key)
{
return Encipher(input, 26 - key);
}
private void Main_Load(object sender, EventArgs e)
{
}
private void btnCipher_Click(object sender, EventArgs e)
{
Random rndNumber = new Random();
tbKey.Text = rndNumber.Next(0, 26).ToString();
originalText = tbWord.Text;
try {
key = Convert.ToInt32(tbKey.Text);
}
catch {
MessageBox.Show("Invalid Input! Please Try Again");
}
cipherText = Encipher(originalText, key);
tbOutput.Text = Convert.ToString(cipherText);
}
private void btnDecipher_Click(object sender, EventArgs e)
{
DecipherText = tbOutput.Text;
string DecipherOutput = Decipher(cipherText, key);
tbDecipheredOutput.Text = Convert.ToString(DecipherOutput);
}
private void btnOpen_Click(object sender, EventArgs e)
{
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
}
}
tbOutput.Text = fileContent;
MessageBox.Show("Path of File: " + filePath);//shows the path of the file
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";//the format the file will be saved in
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//saves the folder to my documents
string Content = tbOutput.Text;//saves the contens of textbox output
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
File.WriteAllText((saveFileDialog1.FileName), Content);
}
catch (IOException)
{
MessageBox.Show("File Was not Saved");
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace login_form
{
class tuple
{
int key;
string cipherText;
static public void Main()
{
// Creating tuple with two elements
// Using Create method
var My_Tuple = Tuple.Create("lol", 23);
Console.WriteLine("Element 1: " + My_Tuple.Item1);
Console.WriteLine("Element 2: " + My_Tuple.Item2);
}
}
}

(C# Windows Form) Convert and Save File to PDF using Microsoft Print to PDF

I did a windows form using C# about Conversion File to PDF using Microsoft Print to PDF. I already referred to many coding and one of it is from Unable to open the PDF, which was generated using print to pdf code written in C#.
Here below is what I made some little changes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using Microsoft.Office.Interop.Word;
//using Microsoft.Office.Interop.Excel;
//using Spire.Pdf;
namespace fileConversion
{
public partial class Form1 : Form
{
private System.Drawing.Font printFont;
private StreamReader streamToPrint;
public Form1()
{
InitializeComponent();
}
private void selectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName; //text label1 tu pegang file name yang kita select
}
}
private void convertFile_Click(object sender, EventArgs e)
{
string FileName = label1.Text;
// the directory to store the output.
//string directoryPath = System.IO.Path.GetDirectoryName(FileName);
// initialize PrintDocument object
PrintDocument doc = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// set the filename to whatever you like (full path)
PrintFileName = System.IO.Path.GetFullPath(#"" + FileName + ".pdf"),
// tell the object this document will print to file
PrintToFile = true,
}
};
doc.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
doc.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = File.ReadAllText(label1.Text);
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while ((line = streamToPrint.ReadLine()) != null)
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
}
However, based on what I did (above code), I successfully convert the file to pdf, but it is unable to open. Before I changed above code, I did:
private void convertFile_Click(object sender, EventArgs e)
{
string FileName = label1.Text;
// the directory to store the output.
//string directoryPath = System.IO.Path.GetDirectoryName(FileName);
// initialize PrintDocument object
PrintDocument doc = new PrintDocument()
{
PrinterSettings = new PrinterSettings()
{
// set the printer to 'Microsoft Print to PDF'
PrinterName = "Microsoft Print to PDF",
// set the filename to whatever you like (full path)
PrintFileName = System.IO.Path.GetFullPath(#"" + FileName + ".pdf"),
// tell the object this document will print to file
PrintToFile = true,
}
};
doc.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
doc.Print();
}
Though the conversion from file to pdf are success, but the contents in the pdf file are not displayed. there's only blank pdf file.
and this is my new code after corrected.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace fileConversion
{
public partial class Form1 : Form
{
private static Font printFont;
private static StreamReader streamToPrint;
public Form1()
{
InitializeComponent();
}
private void selectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName; //text label1 tu pegang file name yang kita select
}
}
private void convertFile_Click(object sender, EventArgs e)
{
try
{
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
streamToPrint = new StreamReader(#"" + label1.Text);
PrintDocument doc = new PrintDocument();
if (printDialog1.PrinterSettings.PrinterName == "Microsoft Print to PDF")
{ // force a reasonable filename
string filename = Path.GetFileNameWithoutExtension(label1.Text);
string directory = Path.GetDirectoryName(label1.Text);
doc.PrinterSettings.PrintToFile = true;
// confirm the user wants to use that name
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = directory;
saveFileDialog1.FileName = filename + ".pdf";
saveFileDialog1.Filter = "PDF File|*.pdf";
result = saveFileDialog1.ShowDialog();
if (result != DialogResult.Cancel)
doc.PrinterSettings.PrintFileName = saveFileDialog1.FileName;
}
if (result != DialogResult.Cancel) // in case they canceled the save as dialog
{
doc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
doc.Print();
}
}
}
finally
{
streamToPrint.Close();
}
}
private static void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Iterate over the file, printing each line.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
}

How do I clear the loaded assembly so that when i reload a plugin, it will not duplicate?

I made it so that you can open a dll and load it onto a listbox as a clickable item when once clicked, the plugin would load and you can execute that plugin.
I added a clear button that is suppose to clear the app of the currently loaded plugins.
It clears everything off the app but when I go to load them again, it loads duplicates.
How do I clear the assembly too so that when i reload a plugin, it will not duplicate??
Code Behind:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using PluginContracts;
using System;
using System.IO;
using Microsoft.Win32;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Diagnostics;
using System.Linq;
namespace SimplePlugin
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Dictionary<string, IPlugin> _Plugins; // move to class scope
public MainWindow()
{
InitializeComponent();
_Plugins = new Dictionary<string, IPlugin>();
}
private void AssembleComponents(object sender)
{
string selection = "";
if (sender is ListBox)
{
if (((ListBox)sender).SelectedValue != null)
selection = ((ListBox)sender).SelectedValue.ToString();
}
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
DirectoryCatalog cat = new DirectoryCatalog(path);
//ICollection<IPlugin> plugins = PluginLoader.LoadPlugins("Plugins");
ICollection<IPlugin> plugins = GenericPluginLoader<IPlugin>.LoadPlugins("Plugins");
foreach (var item in plugins)
{
//add only if not already present
if (!_Plugins.ContainsKey(item.Name))
{
string dllName = GetDLLName(item.Name);
Button b = new Button()
{
Name = dllName.Replace(".", "").ToUpper(),
Content = item.Name,
Visibility = System.Windows.Visibility.Hidden
};
b.Click += b_Click;
PluginGrid.Children.Add(b);
_Plugins.Add(item.Name, item);
// this.PluginGrid.Children.Clear();
//by Vasey
}
}
// make visible the selected plugin button
foreach (var ctl in PluginGrid.Children)
{
if (ctl is Button)
{
Button button = (Button)ctl;
if (button.Name.Equals(selection.Replace(".", "").ToUpper()))
{
button.Visibility = System.Windows.Visibility.Visible;
}
else
{
button.Visibility = System.Windows.Visibility.Hidden;
}
}
}
}
private void b_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
if (b != null)
{
string key = b.Content.ToString();
if (_Plugins.ContainsKey(key))
{
IPlugin plugin = _Plugins[key];
plugin.Do();
}
}
}
private void addPlugin_Click(object sender, RoutedEventArgs e)
{
var fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true;
fileDialog.Filter = "All files (*.*)|*.*|DLL files (*.dll)|*.dll|CS Files (*.cs)|*.cs";
if (fileDialog.ShowDialog() == true)
{
string filename = fileDialog.FileName;
var ext = System.IO.Path.GetExtension(filename);
// ListBox lbFiles = new ListBox();
//this.Controls.Add(lbFiles);
//lbFiles.Size = new System.Drawing.Size(200, 100);
//lbFiles.Location = new System.Drawing.Point(10, 10);
lbFiles.Items.Add(System.IO.Path.GetFileName(filename));
//
CopyToDir(filename);
}
}
private void CopyToDir(string filename)
{
// txtBox.Text = "Hello World";
string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
Console.WriteLine(path);
//Check the directory exists
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
try
{
FileInfo fi = new FileInfo(filename);
if (!File.Exists(System.IO.Path.Combine(path, fi.Name)))
{
File.Copy(fi.FullName, System.IO.Path.Combine(path, fi.Name));
}
}
catch (Exception ex)
{
throw ex;
}
}
// Get linkage between ListBox's DLL name list and the loaded plugin names
string GetDLLName(string name)
{
string ret = "";
name = name.Replace(" ", ""); // strip spaces
Assembly asm = AppDomain.CurrentDomain.GetAssemblies().
SingleOrDefault(assembly => assembly.GetName().Name == name);
if (asm != null)
{
ret = Path.GetFileName(asm.Location);
}
return ret;
}
private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
AssembleComponents(sender);
}
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
lbFiles.Items.Clear();
_Plugins = new Dictionary<string, IPlugin>();
}
}
}
I just had to add this function to the clearBtn method
//Clears the Assembly
this.PluginGrid.Children.Clear();
Before
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
lbFiles.Items.Clear();
_Plugins = new Dictionary<string, IPlugin>();
}
After
private void ClearBtn_Click(object sender, RoutedEventArgs e)
{
// Clears the ListBox
lbFiles.Items.Clear();
//Clears the Assembly
this.PluginGrid.Children.Clear();
//Loads next Assembly
_Plugins = new Dictionary<string, IPlugin>();
}

Outlook 2010 addins for automatic email-attachment extraction

Outlook 2010 add in for auto extraction of attachment from the configured email ids on outlook 2010.For each configured email ids there will be a separate folder in which it's attachments will be saved automatically.I don't want to click any button or reload it all the times.If a mail arrives in the inbox folder,if it's unread, it's attachments will be extracted and saved in its respective folder.
My problem is that i haven't been able to extract the attachments of non-default email ids on outlook 2010 and also my process is not automatically extracting the attachment.
How can i make the unread mail's attachment extraction and saving automatic for multiple configured email ids on outlook 2010? Here i'm attaching the code which i have tried....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.IO;
namespace ITAPOutlookAddIn
{
public partial class ThisAddIn
{
public void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new Microsoft.Office.Interop.Outlook
.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
this.Application.NewMail += new Microsoft.Office.Interop.Outlook
.ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMailStatus);
}
public void ThisApplication_NewMail()
{
const string destinationDirectory = #"C:\TestFileSave";
const string destinationDirectory2 = #"C:\TestFileForm";
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
if (!Directory.Exists(destinationDirectory2))
{
Directory.CreateDirectory(destinationDirectory2);
}
Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
.Session.GetDefaultFolder(Outlook
.OlDefaultFolders.olFolderInbox);
Outlook.Items inBoxItems = inBox.Items;
Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");
try
{
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Outlook.MailItem;
if (newEmail == null)
continue;
if (newEmail != null)
{
if (newEmail.ReceivedByName == "Sumit Ray")
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
string filepath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName);
string Sname = newEmail.SentOnBehalfOfName;
string timestamp = newEmail.ReceivedTime.ToString("MMddyyyy.HHmmss");
string result = filepath + Sname + timestamp + ".docx";
newEmail.Attachments[i].SaveAsFile(result);
// newEmail.Attachments[i].SaveAsFile
// (#"C:\TestFileSave\" +
// newEmail.Attachments[i].FileName);
}
}
} //end of inner-if
} //end of outer-if
} //end of for-each
}//end of try
catch (Exception ex)
{
Console.WriteLine(ex);
string errorInfo = (string)ex.Message
.Substring(0, 11);
if (errorInfo == "Cannot save" && newEmail.SenderName == "Sumit Ray")
{
MessageBox.Show(#"Create Folder C:\TestFileSave");
}
} //end of catch void ThisApplication_NewMailStatus()
{
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
// Mark each unread message from Jeff Hay with a yellow flag icon.
Outlook.Items unreadMailItems =
inbox.Items.Restrict("[Unread]= true");
// if (Convert.ToBoolean(unreadMailItems)
if(unreadMailItems.Equals(true))
{
ThisApplication_NewMail();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Do you mean you need to read unread messages from a non-default store? Instead of using outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox), loop through the Namespace.Stores collection and call Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).

Reading Text Files in FileSystemWatcher in C# getting Error of File already in Use by another resource

Hello I want to use FileSystemWatcher in C Sharp to watch for the text files coming in a folder Reading There Text and uploading Their text to a Web Server with a GET Request in C Sharp
but the problem is that when i try it and first time when some file opened it works fine but on second time when a file come to the directory it will show me that the file is already used by another application or the resource is not free its already allocated.
here is the small code for it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace FileChangeNotifier
{
public partial class frmNotifier : Form
{
private StringBuilder m_Sb;
private bool m_bDirty;
private System.IO.FileSystemWatcher m_Watcher;
private bool m_bIsWatching;
public frmNotifier()
{
InitializeComponent();
m_Sb = new StringBuilder();
m_bDirty = false;
m_bIsWatching = false;
}
private void btnWatchFile_Click(object sender, EventArgs e)
{
if (m_bIsWatching)
{
m_bIsWatching = false;
m_Watcher.EnableRaisingEvents = false;
m_Watcher.Dispose();
btnWatchFile.BackColor = Color.LightSkyBlue;
btnWatchFile.Text = "Start Watching";
}
else
{
m_bIsWatching = true;
btnWatchFile.BackColor = Color.Red;
btnWatchFile.Text = "Stop Watching";
m_Watcher = new System.IO.FileSystemWatcher();
if (rdbDir.Checked)
{
m_Watcher.Filter = "*.*";
m_Watcher.Path = txtFile.Text + "\\";
}
else
{
m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1);
m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length);
}
if (chkSubFolder.Checked)
{
m_Watcher.IncludeSubdirectories = true;
}
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.EnableRaisingEvents = true;
}
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
readFile(e.FullPath);
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
}
}
private void readFile(String filename) {
String line = "";
if (File.Exists(filename))
{
try{
StreamReader sr = new StreamReader(filename);
//code for multiline reading but i need only one line so i am going to change he code
/* while ((line = sr.ReadLine()) != null)
{
MessageBox.Show(line);
}
*/
line = sr.ReadLine();
MessageBox.Show(line);
uploadDataToServer(line);
sr.Close();
} catch(IOException e){
MessageBox.Show(e.Message);
}
}
}
private void uploadDataToServer(String data) {
String url = "http://209.90.88.135/~lilprogr/?data="+data;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
}
}
}
To handle multiple change notifications, go here and look for #BaBu's answer.
Also,
If all you need is to read from the file,
Have you tried to open it in Shared Mode?

Categories