Sunday, 1 March 2015

C# switch statement string contains

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Diagnostics;
using System.Linq;
namespace StringEscapeCharacters
{
class Program
{
static string strToAddress;
static string strSubject;
//static string body = " ";
static string strServer;
static string strPort;
static string strFrom;
static string strInputArgs;
static string strLogmsg = string.Empty;
static void Main(string[] args)
{
string[] keyStrings = new string[] { "#to", "#subject", "#server", "#port", "#from" };
foreach (string arg in args)
{
strInputArgs += " " + arg;
//switch start....
foreach (string s in keyStrings)
{
string caseSwitch = "null";
if (arg.Contains(s))
{
caseSwitch = s;
switch (caseSwitch)
{
case "#to":
strToAddress = arg.Split('=')[1];
if (strToAddress.IndexOf("\"") != 1)
{
strToAddress = strToAddress.Replace("\"", "");
}
break;
case "#subject":
strSubject = arg.Split('=')[1];
if (strSubject.IndexOf("\"") != -1)
{
strSubject = strSubject.Replace("\"", "");
}
break;
case "#server":
strServer = arg.Split('=')[1];
if (strServer.IndexOf("\"") != -1)
{
strServer = strServer.Replace("\"", "");
}
break;
case "#port":
strPort = arg.Split('=')[1];
if (strPort.IndexOf("\"") != -1)
{
strPort = strPort.Replace("\"", "");
}
break;
case "#from":
strFrom = arg.Split('=')[1];
if (strFrom.IndexOf("\"") != -1)
{
strFrom = strFrom.Replace("\"", "");
}
break;
case "null":
strLogmsg += DateTime.Now + ": Wrong arg: " + arg + "<br/>";
break;
}
break;
}
}
//Switch end...
}
try
{
//do business logic...
strLogmsg += DateTime.Now + ": Actual arg values: " + strInputArgs + "<br/>";
strLogmsg += DateTime.Now + ": Args after adding escape charecters:" + strToAddress+""+strServer+""+strSubject+""+strPort+""+strFrom;
}
catch (Exception ex)
{
// Log error message
strLogmsg += DateTime.Now + ": Exception : ";
strLogmsg += "Message : " + ex.Message;
strLogmsg += "StackTrace : " + ex.StackTrace;
}
System.IO.File.WriteAllText(@"C:\temp\mylog.log", strLogmsg);
return;
}
}
}
view raw gistfile1.cs hosted with ❤ by GitHub