C# Code Review: My first experience with coding C#
<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>My logic:</p>

<p>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 </p>

<pre><code>text = Regex.Replace(text, args[1], args[2]);
</code></pre>

<p>has an exception and if I have </p>

<pre><code>WriteLog(args[0], args[1], args[2], strException, intStatus);
</code></pre>

<p>also in the same try block, it will not execute and I will not have any logs.</p>

<p>Also, if I have the </p>

<pre><code>WriteLog(args[0], args[1], args[2], strException, intStatus);
</code></pre>

<p>in the catch block, any exception in that method cannot be caught. Yes I can have try-catch in the <code>WriteLog</code> method but I see no problems in my approach too.</p>

<p>So first, I have a try-catch for file manipulation and then another for log write.</p>

<p>Please ignore the log path(hardcoded as D:).</p>

<pre><code>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 &lt; 3)
        {
            intStatus = 1;
            WriteLog("", "", "", "Improper Arguments", intStatus);
            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();
            WriteLog(args[0], args[1], args[2], strException, intStatus);
            intStatus = 1;
        }

        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() + " (0 for PASS, 1 for FAIL)");
        GrepLog.WriteLine("Exception: " + exception);
        GrepLog.Close();
    }
}
</code></pre>
