Monday, June 29, 2009

C# Read text from .doc to c# application

Read text from word file (.doc)

Add referance to MicroSoft Word 11.0 object library

Word.ApplicationClass wordApp = new Word.ApplicationClass();
string filePath = "c:\\tempfile.doc";
object file = filePath;
object nullobj = System.Reflection.Missing.Value;
object nosave = Word.WdSaveOptions.wdDoNotSaveChanges;
Word.Document doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
string m_Content = doc.Content.Text;
doc.Close(ref nosave, ref nullobj, ref nullobj);
wordApp.Quit(ref nosave, ref nullobj, ref nullobj);

richTextBox.Text = m_Content;

Saturday, June 27, 2009

Compare tables with same data

SQL Query Compare data in two tables having same data

(SELECT * FROM @Table1 EXCEPT SELECT * FROM @Table2) UNION (SELECT * FROM @Table2 EXCEPT SELECT * FROM @Table1)

Will return unque records, if the query returned no records, the data in both table are same.

Thursday, June 25, 2009

c# .net - Delete files from local drive

try
{
Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile("new2");
}
catch
{
// file does not exist
}

Wednesday, June 24, 2009

C# .net – write text into a file

// create stream writer
StreamWriter writer = new StreamWriter("new");
try
{
writer1 = new StreamWriter("C:\\Program Files\\pdf995\\res\\PDF995.ini");
}
catch
{
file does not exist write to another file
}

Tuesday, June 23, 2009

c# Word automation - simplest way to create word doc in windows application

WordAuto word = new WordAuto();
ArrayList array = new ArrayList();

array.Insert(0,Name.Text);
array.Insert(1,Age.Text);
array.Insert(2,Designation.Text);

word.CreateFile(array);

Monday, June 22, 2009

Read word document proprties

string filePath = "c:\\docprop.doc";
object file = filePath;
object nullobj = System.Reflection.Missing.Value;
Word.Document oDoc = oWord.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

oDocBuiltInProps = oDoc.BuiltInDocumentProperties;

Optical Character Recongnition

Excellent article on OCR (optical character recongnition application using artificial neural networks)

http://www.codeproject.com/KB/recipes/UnicodeOCR.aspx

C# Send mail - Windows application

Outlook.Application oApp = new Outlook.Application();

//Create the new message by using the simplest approach.
Outlook.MailItem oMsg= Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

//Add a recipient
Outlook.Recipient oRecip;
oRecip = (Outlook.Recipient)oMsg.Recipients.Add("xyz@xyz.com");

//Set the basic properties.
oMsg.Subject = "Payment received";
oMsg.Body = "Dear Sir, We have received payment for your order";

//Send the message.
oMsg.Save();
oMsg.Send();

//Explicitly release objects.
oRecip = null;
//oAttach = null;
oMsg = null;
oApp = null;

Friday, June 19, 2009

Converting a normal IP Address to IP Long/decimal Format

public long ip2long(string ip)
{
string[] parts = ip.Split('.');
string strhexip = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}",
int.Parse(parts[0]), int.Parse(parts[1]),
int.Parse(parts[2]), int.Parse(parts[3]));
return Convert.ToInt64(strhexip, 16);
}

Read IP address of visitor

string ip = Request.ServerVariables["REMOTE_ADDR"];

Turn off cookies for one page in your site

Cookie.Discard Property which Gets or sets the discard flag set by the server

Thursday, June 18, 2009

C# Clone an object

public static void Main(Strings[] args)
{
Point pt1=new Point(150,160);
Point pt2=(Point)pt1.Clone(); //This will return new new object
}

Retrieve current user desktop path

c# .Net

string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);