I had to create an executable to search and replace strings in a file. This is to be used in my installer for text file manipulation. I have various placeholders in configuration files that I need to replace with proper values after querying the end user system.
I have written the following code. This being my first real experience with C#, I do not expect the code to be any good. I have come up with this code based upon a couple of hours of reading MSDN and stackoverflow. I needed someone to review this. There is no one greater than the community to do this.
Please review this and let me know what modification are necessary. I am specifically concerned about the exception handling. I have included two try-catch blocks.
My logic:
If I have all operations under one try-catch block, the failure of one process will block the execution of all others. That is, if my
text = Regex.Replace(text, args[1], args[2]);
has an exception and if I have
WriteLog(args[0], args[1], args[2], strException, intStatus);
also in the same try block, it will not execute and I will not have any logs.
Also, if I have the
WriteLog(args[0], args[1], args[2], strException, intStatus);
in the catch block, any exception in that method cannot be caught. Yes I can have try-catch in the WriteLog method but I see no problems in my approach too.
So first, I have a try-catch for file manipulation and then another for log write.
Please ignore the log path(hardcoded as D:).
class Program
{
static int Main(string[] args)
{
//args[0] = The file that needs modification
//args[1] = The string to replace
//args[2] = The string with which to replace args[1]
int intStatus = 0;
string strException = "";
//Get out immediately if no/improper arguments are found
if (args.Length < 3)
{
intStatus = 1;
return intStatus;
}
try
{
StreamReader streamReader = new StreamReader(args[0]);
String text = streamReader.ReadToEnd();
streamReader.Close();
text = Regex.Replace(text, args[1], args[2]);
StreamWriter streamWriter = new StreamWriter(args[0]);
streamWriter.Write(text);
streamWriter.Close();
}
catch (Exception ex)
{
//text file manipulation failed
strException = ex.ToString();
intStatus = 2;
}
try
{
WriteLog(args[0], args[1], args[2], strException, intStatus);
}
catch
{
//log write failed
if (intStatus == 0)
{
intStatus = 3; //if intStatus = 1 when we get here, no need to modify the value(its a complete failure)
//if intStatus = 0 when we get here, make intStatus = 2 so as to clearly distinguish a "log write failed"
//error from a "text file manipulation failed" error
}
}
return intStatus;
}
//The new Logger for the exe
static void WriteLog(string arg0, string arg1, string arg2, string exception, int status)
{
string strGrepLogFileName = string.Format("D:\\TempLogs\\Grep-{0:yyyy-MM-dd_hh-mm-ss-tt}.log", DateTime.Now);
StreamWriter GrepLog = new StreamWriter(strGrepLogFileName, true);
GrepLog.WriteLine("Argument 1: " + arg0);
GrepLog.WriteLine("Argument 2: " + arg1);
GrepLog.WriteLine("Argument 3: " + arg2);
GrepLog.WriteLine("Status: " + status.ToString());
GrepLog.WriteLine("Exception: " + exception);
GrepLog.Close();
}
}
EDIT 1: It is an absolute pity that I cannot mark multiple answers.