Input argumenets
#to=test@gmail.com #subject="Welcome to" #server=mail.gmail.com #port=8080 #from=from@gmail.com
#to="test@gmail.com" #subject="Welcome" #server="mail.gmail.com" #port="8080" #from="from@gmail.com"
#to=\"test@gmail.com\" #subject=\"Welcome\" #server=\"mail.gmail.com\" #port=\"8080\" #from=\"from@gmail.com\"
Expected input string to application.
#to=\"test@gmail.com\" #subject=\"Welcome\" #server=\"mail.gmail.com\" #port=\"8080\" #from=\"from@gmail.com\"
Application will get Input argumenets in any format of give 3 type, but business will work for only one type of format, so we have to manipulate rest of the 2 types to 3rd type.
Solution:
I am going to write one exe, to manipulate input args.
Open Visual studio
Create new console project: StringEscapeCharacters
Open: Program.cs file
Please find my code...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | |
////Using If statment...start | |
//if (arg.StartsWith("#to=")) | |
//{ | |
// strToAddress = arg.Split('=')[1]; | |
// if (strToAddress.IndexOf("\"") != 1) | |
// { | |
// strToAddress = strToAddress.Replace("\"", ""); | |
// } | |
//} | |
//else if (arg.StartsWith("#subject=")) | |
//{ | |
// strSubject = arg.Split('=')[1]; | |
// if (strSubject.IndexOf("\"") != -1) | |
// { | |
// strSubject = strSubject.Replace("\"", ""); | |
// } | |
//} | |
//else if (arg.StartsWith("#from=")) | |
//{ | |
// strFrom = arg.Split('=')[1]; | |
// if (strFrom.IndexOf("\"") != -1) | |
// { | |
// strFrom = strFrom.Replace("\"", ""); | |
// } | |
//} | |
//else if (arg.StartsWith("#server=")) | |
//{ | |
// strServer = arg.Split('=')[1]; | |
// if (strServer.IndexOf("\"") != -1) | |
// { | |
// strServer = strServer.Replace("\"", ""); | |
// } | |
//} | |
//else if (arg.StartsWith("#port=")) | |
//{ | |
// strPort = arg.Split('=')[1]; | |
// if (strPort.IndexOf("\"") != -1) | |
// { | |
// strPort = strPort.Replace("\"", ""); | |
// } | |
//} | |
////Using If statment...end | |
} | |
//This is to add more escape charecters, but right now it is not usefull.. | |
//string strUpdatedArgs = "#to=\\" + "\"" + strToAddress + "\\\" #subject=\\" + "\"" + strSubject + "\\\" #server=\\" + "\"" + strServer + "\\\" #port=\\" + "\"" + strPort + "\\\" #from=\\" + strFrom + "\\\" "; | |
//This is adding escape charecter statically, with out reading args | |
//string strUpdatedArgs="#to=\"to@gmail.com\" #subject=\"Welcome to exe\" #server=\"mail.gmail.com\" #port=\"8080\" #from=\"plmuat1@microsoft.com\""; | |
//This is adding escape charecters dynamically, by reading args | |
string strUpdatedArgs = "#to=" + "\"" + strToAddress + "\" #subject=" + "\"" + strSubject + "\" #server=" + "\"" + strServer + "\" #port=" + "\"" + strPort + "\" #from=" + "\"" + strFrom + "\" "; | |
try | |
{ | |
//do business logic... | |
strLogmsg += DateTime.Now + ": Actual arg values: " + strInputArgs + "<br/>"; | |
strLogmsg += DateTime.Now + ": Args after adding escape charecters:" + strUpdatedArgs; | |
} | |
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; | |
} | |
} | |
} | |
Now i have to pass arguments to my exe, i can to multiple ways..
1st way: Open solution explorer, right click on project.
Click on: Properties
Click on: Debug
Under Start Options:
Command line arguments: <Enter arguments>
Ex:
#to=test@gmail.com #subject="Welcome to exe" #server=mail.gmail.com #port=8080 #from=from@gmail.com
You will get out put as in the log file:C:\Temp\mylog.log
#to="test@gmail.com" #subject="Welcome to exe" #server="mail.gmail.com" #port="8080" #from="from@gmail.com"
2nd way passing args:
Build the application.
now go to bin/debug folder,
you will see: StringEscapeCharacters.exe
execute this exe from cmd and pass args.
ex: StringEscapeCharacters.exe #to=test@gmail.com #subject="Welcome to exe" #server=mail.gmail.com #port=8080 #from=from@gmail.com