Unlike simply calling Process.Start("executable_name.exe","arguments") - which does not pause the parent C# process thread, use the below implementation for waiting for the batch script to finish executing before proceeding with the next set of instructions:
using System.Diagnostics;
Process proc = new Process();
proc.StartInfo.FileName = "C:\\myscript.bat";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
Monday, January 28, 2008
Wednesday, January 23, 2008
C#: Equivalent of JavaScript escape function
Although Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode are available to use in C# out of the box, they can not convert all the characters exactly the same way as JavaScript escape function does. For example let's say original string is: "KC's trick /Hello". Then,
Uri.EscapeDataString("KC's trick /Hello") gives:
"KC's%20trick%20%2FHello"
Uri.EscapeUriString("KC's trick /Hello") gives:
"KC's%20trick%20/Hello"
System.Web.HttpUtility.UrlEncode("KC's trick /Hello") gives:
"KC's+trick+%2fHello"
System.Web.HttpUtility.UrlPathEncode("KC's trick /Hello") gives:
"KC's%20trick%20/Hello"
Hence the full proof solution is to use the JScript.Net's own implementation. In order to do that here's what you need to use:
1. Reference Microsoft.JScript.dll in the project reference
2. Use Microsoft.JScript.GlobalObject.escape function to do the encode.
So finally Microsoft.JScript.GlobalObject.escape("KC's trick /Hello") gives:
"KC%27s%20trick%20/Hello"
Similarly to unescape, use Microsoft.JScript.GlobalObject.unescape function. So Microsoft.JScript.GlobalObject.unescape("KC%27s%20trick%20/Hello") gives back:
"KC's trick /Hello"
Uri.EscapeDataString("KC's trick /Hello") gives:
"KC's%20trick%20%2FHello"
Uri.EscapeUriString("KC's trick /Hello") gives:
"KC's%20trick%20/Hello"
System.Web.HttpUtility.UrlEncode("KC's trick /Hello") gives:
"KC's+trick+%2fHello"
System.Web.HttpUtility.UrlPathEncode("KC's trick /Hello") gives:
"KC's%20trick%20/Hello"
Hence the full proof solution is to use the JScript.Net's own implementation. In order to do that here's what you need to use:
1. Reference Microsoft.JScript.dll in the project reference
2. Use Microsoft.JScript.GlobalObject.escape function to do the encode.
So finally Microsoft.JScript.GlobalObject.escape("KC's trick /Hello") gives:
"KC%27s%20trick%20/Hello"
Similarly to unescape, use Microsoft.JScript.GlobalObject.unescape function. So Microsoft.JScript.GlobalObject.unescape("KC%27s%20trick%20/Hello") gives back:
"KC's trick /Hello"
Friday, January 4, 2008
C#: Send email using SmtpClient
using System.Net.Mail;
//mailFrom > "KSeeSharp" - shows up as "KSeeSharp" as sender
//mailTo > Comma delimited email address(es)
//mailServer > Something like smtp.xyz.com
public bool SendEmail(string mailFrom, string mailTo, string subject, string message, string mailServer)
{
bool returnCode = false;
try
{
MailMessage msg = new MailMessage(mailFrom, mailTo, subject, message);
SmtpClient emailClient = new SmtpClient(mailServer);
emailClient.UseDefaultCredentials = true;
emailClient.Send(msg);
returnCode = true;
}
catch (Exception ex)
{
throw ex
}
return returnCode;
}
//mailFrom > "KSeeSharp
//mailTo > Comma delimited email address(es)
//mailServer > Something like smtp.xyz.com
public bool SendEmail(string mailFrom, string mailTo, string subject, string message, string mailServer)
{
bool returnCode = false;
try
{
MailMessage msg = new MailMessage(mailFrom, mailTo, subject, message);
SmtpClient emailClient = new SmtpClient(mailServer);
emailClient.UseDefaultCredentials = true;
emailClient.Send(msg);
returnCode = true;
}
catch (Exception ex)
{
throw ex
}
return returnCode;
}
Subscribe to:
Posts (Atom)