Monday, February 27, 2012
Wednesday, February 22, 2012
How to open a file from C# ?
This example demonstrates how to open file with an associated program. It shows,
how to open a file and how to open url address in a default web browser.In c#
files are opened using Process.Start method .
// open a file
System.Diagnostics.Process.Start(@"c:\letter.pdf");
//Open a URL in default browser
System.Diagnostics.Process.Start("http://snippets4you.blogspot.com");
Saturday, February 18, 2012
How to call C# com from Java Script ?
This code snippets helps you to create a c# com and call fuction in c# com from javascript code .Add a reference runtime.InteropServices.Note that this code only works with internet explorer.
C# COM :
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TestCOM
{
[Guid("F7B3192E-8B84-4cf0-9162-95166783299A")]
public interface COM_Interface
{
int disp(int aa);
}
[Guid("879A7994-1FE9-4379-8F84-C135B59B5C4B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface COM_Events
{
}
[Guid("E723167E-2244-4c9d-9681-F2E2108E49D3"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(COM_Events))]
public class COM_Class : COM_Interface
{
public int disp(int aa)
{
return aa + 2;
}
}
}
Java script code :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> CSHARP COM TEST </TITLE>
<script type='text/javascript' language='javascript'>
function comFunction()
{
try
{
var myobject;
myobject = new ActiveXObject("TestCOM.COM_Class");
alert(myobject.disp(2)); // Call DLL function here.
}
catch(e)
{
alert(e.message);
}
}
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE=button value=DLL onClick="comFunction()">
<br>
</BODY>
</HTML>
C# COM :
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TestCOM
{
[Guid("F7B3192E-8B84-4cf0-9162-95166783299A")]
public interface COM_Interface
{
int disp(int aa);
}
[Guid("879A7994-1FE9-4379-8F84-C135B59B5C4B"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface COM_Events
{
}
[Guid("E723167E-2244-4c9d-9681-F2E2108E49D3"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(COM_Events))]
public class COM_Class : COM_Interface
{
public int disp(int aa)
{
return aa + 2;
}
}
}
Java script code :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> CSHARP COM TEST </TITLE>
<script type='text/javascript' language='javascript'>
function comFunction()
{
try
{
var myobject;
myobject = new ActiveXObject("TestCOM.COM_Class");
alert(myobject.disp(2)); // Call DLL function here.
}
catch(e)
{
alert(e.message);
}
}
</SCRIPT></HEAD>
<BODY>
<INPUT TYPE=button value=DLL onClick="comFunction()">
<br>
</BODY>
</HTML>
Wednesday, February 8, 2012
How do I send mail using C# ?
The following example demonstrates sending a simple text email :
private void buttonX1_Click(object sender, EventArgs e)
{
string strFrom, strTo,strFeedback;
try
{
System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage();
strFrom = textBoxX2.Text;
strFeedback = richTextBox1.Text;
objMailMessage.To.Add("jakesully25@gmail.com");
objMailMessage.From = new System.Net.Mail.MailAddress(strFrom);
objMailMessage.Subject = "HTML5Point";
objMailMessage.Body = strFrom+" : "+strFeedback;
objMailMessage.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient objSmtpClient = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
objSmtpClient.EnableSsl = true;
objSmtpClient.Credentials = new System.Net.NetworkCredential("jakesully25@gmail.com","server123");
objSmtpClient.Send(objMailMessage);
MessageBox.Show("Feedback Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Note : Add reference System.Net.dll
Subscribe to:
Posts (Atom)