private QifAccount getAccountData(string data)
        {
            QifAccount lastAccount = null;

            string[] accArray = data.Split(new string[] { "\n^" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string accInfo in accArray)
            {
                string[]   properties = accInfo.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                QifAccount newAccount = new QifAccount();

                foreach (string property in properties)
                {
                    switch (property[0])
                    {
                    case 'N':        //N	Name
                        newAccount.Name = property.Substring(1);
                        break;

                    case 'T':        //T    Type of account
                        newAccount.Type = property.Substring(1);
                        break;

                    case 'D':        //D    Description
                        newAccount.Description = property.Substring(1);
                        break;

                    case '$':        //$    Statement balance
                        newAccount.StatementBalance = Convert.ToDecimal(property.Substring(1));
                        break;

                    case 'L':        //L    Credit limit (only for credit card account)
                        newAccount.CreditLimit = Convert.ToDecimal(property.Substring(1));
                        break;

                    case '/':         ///   Statement balance date
                        newAccount.StatementBalanceDate = myStringToDate(property.Substring(1));
                        break;

                    default:
                        throw new Exception("Not handled " + property);
                    } // End switch
                }     // End foreach property

                lastAccount = this.addQifAccount(newAccount);
            }// End foreach accInfo

            return(lastAccount);
        }
        private QifAccount addQifAccount(QifAccount newAccount)
        {
            foreach (QifAccount acc in this.qifAccounts)
            {
                if (acc.Name == newAccount.Name)
                {
                    acc.Type                 = longerString(acc.Type, newAccount.Type);
                    acc.Description          = longerString(acc.Description, newAccount.Description);
                    acc.StatementBalance     = beterDecimal(acc.StatementBalance, newAccount.StatementBalance);
                    acc.CreditLimit          = beterDecimal(acc.CreditLimit, newAccount.CreditLimit);
                    acc.StatementBalanceDate = beterDate(acc.StatementBalanceDate, newAccount.StatementBalanceDate);
                    return(acc);
                }
            }

            this.qifAccounts.Add(newAccount);
            return(newAccount);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////
        //   Phase 1 - Read the QIF file and store the data in the more usable qifAccounts list.
        //   We are using the information we get from the list of accounts in the QIF file and the
        //   transactions assosiated with each account. From this we are also getting all the
        //   catagory names that were being used.
        ////////////////////////////////////////////////////////////////////////////////////////////
        private void readHeaders()
        {
            int        complete = 0;
            int        count    = 0;
            TextReader inFile;
            QifAccount lastAccount = null;

            // Read in the file and split into strings by headers that start with a "\n!"
            inFile = new StreamReader(fileName);

            // Skip the first char '!' so it looks like the rest of the headers that will be missing the '!'
            inFile.Read();
            string [] sections = inFile.ReadToEnd().Split(new  string[] { "\n!" }, StringSplitOptions.RemoveEmptyEntries);
            inFile.Close();
            count = sections.Length;

            foreach (string section in sections)
            {
                string firstLine;
                string data = "";

                // Divide the section into the first line and the rest of the data
                int charIndex = section.IndexOfAny(new char[] { '\r', '\n' });

                if (charIndex > 0)
                {
                    firstLine = section.Substring(0, charIndex);
                    data      = section.Substring(charIndex).Replace("\r", "").Trim();
                }
                else
                {
                    firstLine = section;
                    data      = "";
                }

                // decide what to do according to the first line of the section
                switch (firstLine)
                {
                case "Account":
                    lastAccount = getAccountData(data);
                    break;

                case "Type:Bank ":
                    getTransactionData(lastAccount, data);
                    break;

                case "Type:Cash":
                    getTransactionData(lastAccount, data);
                    break;

                case "Type:CCard":
                    getTransactionData(lastAccount, data);
                    break;

                case "Type:Oth A":
                    getTransactionData(lastAccount, data);
                    break;

                case "Type:Oth L":
                    getTransactionData(lastAccount, data);
                    break;

                default:
                    break;
                } // end switch

                this.importWorker.ReportProgress((complete += 100) / count, "Reading Qif File.");
            }// END foreach section
        }
        private void getTransactionData(QifAccount account, string data)
        {
            string[] transArray = data.Split(new string[] { "\n^" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string transInfo in transArray)
            {
                string[]       properties     = transInfo.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                QifTransaction newTransaction = new QifTransaction();
                QifSplit       newSplit       = null;

                foreach (string property in properties)
                {
                    switch (property[0])
                    {
                    case 'D':        //D    Date
                        newTransaction.Date = myStringToDate(property.Substring(1));
                        continue;

                    case 'T':        //T    Amount
                        newTransaction.Amount = Convert.ToDecimal(property.Substring(1));
                        continue;

                    case 'C':        //C    Cleared status
                        newTransaction.ClearedStatus = property.Substring(1);
                        continue;

                    case 'N':        //N    Num (check or reference number)
                        newTransaction.Num = property.Substring(1);
                        continue;

                    case 'M':        //M    Memo
                        newTransaction.Memo = property.Substring(1);
                        continue;

                    case 'P':        //P    Payee
                        newTransaction.Payee = property.Substring(1);
                        continue;

                    case 'A':        //A    Address (up to five lines; the sixth line is an optional message)
                        newTransaction.Address += property.Substring(1);
                        continue;

                    case 'L':        //L    Category (Category/Subcategory/Transfer/Class)
                        newTransaction.Catagory = property.Substring(1);
                        continue;

                    case 'S':        //S    Category in split (Category/Transfer/Class)
                        newSplit          = new QifSplit();
                        newSplit.Catagory = property.Substring(1);
                        continue;

                    case 'E':         //E   Memo in split
                        newSplit.Memo = property.Substring(1);
                        continue;

                    case '$':        //$    Dollar amount of split
                        newSplit.Amount = Convert.ToDecimal(property.Substring(1));
                        if (newSplit.Catagory.StartsWith("["))
                        {
                            newTransaction.OppLines.Add(newSplit);
                        }
                        else
                        {
                            newTransaction.EnvLines.Add(newSplit);
                        }
                        continue;

                    case 'U':        //Don't know. Always seams to be the same as T (The amount).
                        continue;

                    default:
                        throw new Exception("Not handled " + property);
                    } // End switch
                }     // End foreach property

                account.Transactions.Add(newTransaction);
            }// End foreach accInfo
        }