I am new to writing C# programs, actually new to object oriented programming. I normally write programs in PICK. But I have a software package that will call a pre-processor to allow us to modify a text file before it processes a the file from a 3rd party. I have the process working as a stand alone program, but when calling it through the pre-processor it needs to recieve a value back before it will continue the process. I am trying to pass back a "0", but the pre-processor isn't recieving anything. Can anyone give me a hint as to why my program isn't passing anything back?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class Program
{
//static int Main(string[] args, int good)

static int Main(string [] args)
//static void Main()
{
    if (args.Length != 0)
   {
        //Console.WriteLine("input , {0}", args[0]);
        //string origFileName = "csharptest.txt";
       //good = 0;
        string origFileName = args[0];
        string origPath = @"C:\Users\mshepard\test";
        string strFileName = System.IO.Path.Combine(origPath, origFileName);
        FileStream objFilename = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

        StreamWriter sw = new StreamWriter(objFilename);

        StreamReader objFileRead = new StreamReader(objFilename);

        System.Text.StringBuilder listLines = new System.Text.StringBuilder();

        try
        {
            string strFile = "";

            while ((objFileRead.Peek() != -1))
            {
                string[] strValue = objFileRead.ReadLine().Split(new char[] { '|' });

                int i = 0;
                bool boolFlag = false;
                System.Text.StringBuilder strB = new System.Text.StringBuilder();
                string s_mod;
                foreach (string s in strValue)
                {
                    s_mod = s;
                    if (i == 7)
                    {
                        if (s.ToString() == "INV")
                        {
                            boolFlag = true;
                        }
                    }
                    if (i == 18 & boolFlag == true)
                    {
                        s_mod = "000000.gif";
                    }
                    strB.Append(s_mod + "|");
                    i += 1;
                }
                strFile += strB.ToString().Substring(0, strB.Length - 1) + "\r\n";
            }
            string tempFileName = "temp.txt";
            string tempFileLoc = System.IO.Path.Combine(origPath, tempFileName);
            System.IO.File.WriteAllText(tempFileLoc, strFile);
            System.IO.File.Copy(tempFileLoc, strFileName, true);
            System.IO.File.Delete(@"C:\Users\mshepard\test\temp.txt");

            sw.Flush();
            sw.Close();


        }
        catch (Exception ex)
        {
            objFileRead.Close();
            objFilename.Close();

        }
        finally
        {
            objFileRead.Close();
            objFilename.Close();
        }
        //Environment.Exit(0);

        return 0;
    }
    //Environment.Exit(0);

    return 0;
}

}

I'm confused.
Are you trying to call a process or parse a file?

...or are you trying to modify the file, output a new file THEN call a process?

Do you get NOTHING back from the program calling this one?
Is it a native app?
Have you made another test program that ALL it does is return 0 to see if that one wil work?

BTW: Here are some things that might make the program a little easier:

//UNTESTED
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DW_419357_CS_CON
{
   class Program
   {
      static int Main(string [] args)
      {
         int intRetVal = 0;

         string strOrigPath = @"C:\Users\mshepard\test";

         if (args.Length < 1)
         {
            Console.WriteLine("Program {0}", Path.Combine(strOrigPath, "inputFile.txt"));
            return -1;
         }

         string strFileName = Path.Combine(strOrigPath, args[0]);

         if (!File.Exists(strFileName))
         {
            Console.WriteLine("Input file {0} does not exist.", strFileName);
            return -1;
         }

         StreamReader fileIn = new StreamReader(strFileName);
         StringBuilder listLines = new StringBuilder();
         try
         {
            List<string> lst_strData = new List<string>();
            while(!fileIn.EndOfStream)
            {
               string[] arr_strValue = fileIn.ReadLine().Split('|');
               int i = 0;
               bool blnGotInv = false;
               StringBuilder strB = new StringBuilder();
               string s_mod = string.Empty;

               foreach (string s in arr_strValue)
               {
                  s_mod = s;
                  if (i.Equals(7) && (s.Equals("INV")))
                  {
                     blnGotInv = true;
                  }
                  else if (i.Equals(18) && blnGotInv)
                  {
                     s_mod = "000000.gif";
                  }

                  strB.Append(s_mod + "|");

                  i++;
               }

               lst_strData.Add(strB.ToString().Trim());
            }

            string strTempFileName = Path.GetTempFileName();
            File.WriteAllText(strTempFileName, string.Join("\r\n", lst_strData.ToArray()));
            File.Copy(strTempFileName, strFileName, true);
            File.Delete(strTempFileName);
         }
         catch (Exception exc)
         {
            Console.WriteLine(exc.Message);
            intRetVal = -1;
         }

         return intRetVal;
      }
   }
}

Edited 4 Years Ago by thines01: formatting

A little more background on what I am trying to do. I recieve a pipe delimted file from a 3rd party that needs to be modified before I process it. So I need to check to see if a certian value is blank, if so, I default in 000000.gif. Then I write the file back out to the original location. This is working as a stand alone program. What I need to have happen is get another application to call this program, and pass back a value saying the program is done. So to answer your question, I am processing the file and trying to return a 0 to tell the call program it can continue. I have not created a test program that just passes back a 0, I will try that. I should have thought of that before. Thanks.

This article has been dead for over six months. Start a new discussion instead.