Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:40:34+00:00 2026-05-20T23:40:34+00:00

Hi all i am having a list box on my Form which will display

  • 0

Hi all i am having a list box on my Form which will display all the .txt files from the directory C:. This list box selection mode is set to MultiExtended.

My condition to check whether the file is valid or not will be checked using the condition as each and every line content of the selected file should be *94*. If this one satisifes then only it is said to be a valid file. I have written a code for this too but as i am checking in the loop and at a time i can only read one file content this was working fine. But i have to check initially all the selected files matches the condition or not if ok then i have to do the remaining code if not i would like to display error

My code on Button Click

  private void btnMerge_Click(object sender, EventArgs e)
    {
        if (lstACH.SelectedIndices.Count == 1)
        {
            MessageBox.Show("Select 2 Files To Merge");
        }
        else
        {
            for (i = 0; i < lstACH.SelectedItems.Count; i++)
            {
                strFile = lstACH.SelectedItems[i].ToString();
                lines = File.ReadAllLines(strFile);
                if (LinesHaveCorrectLength(lines, 94)) // Here i am checking but at a tym i am checking one file only i have to check for all and if ok then the remaining code has to be executed
                {
                    if (i == 0)
                    {
                        Stream myStream;

                        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                        saveFileDialog1.InitialDirectory = @"C:\";
                        saveFileDialog1.DefaultExt = "txt";
                        saveFileDialog1.Filter = "(*.txt)|*.txt";
                        saveFileDialog1.FilterIndex = 2;
                        saveFileDialog1.RestoreDirectory = true;
                        saveFileDialog1.ValidateNames = true;
                        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            Append.FileName = saveFileDialog1.FileName;
                            if (Append.FileName.Contains(" \\/:*?<>|"))
                            {
                                MessageBox.Show("File name should not contain \\/:*?<>|", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            else
                            {
                                if ((myStream = saveFileDialog1.OpenFile()) != null)
                                {
                                    Append.FileName = saveFileDialog1.FileName;
                                    myStream.Close();
                                }
                            }
                        }
                    }
                    using (StreamWriter sw = new StreamWriter(Append.FileName, true))
                    {
                        using (StreamReader srBatch = new StreamReader(strFile))
                        {
                            while (srBatch.Peek() >= 0)
                            {
                                strReadLine = srBatch.ReadLine();
                                if (strReadLine.StartsWith("1"))
                                {
                                    if (i == 0)
                                    {
                                        strFileHeader = strReadLine;
                                        sw.WriteLine(strFileHeader);
                                    }
                                }
                                if (strReadLine.StartsWith("5"))
                                {
                                    strBtchHeader = strReadLine;
                                    if (i == 0)
                                    {
                                        Btchno = Convert.ToInt32(strReadLine.Substring(87, 7));
                                        BatchCnt = Convert.ToInt16(Btchno);
                                    }
                                    if (i > 0)
                                    {
                                        BatchCnt++;
                                        strBtchHeader = strBtchHeader.Substring(0, 87) + Convert.ToString(BatchCnt.ToString().PadLeft(7, (char)48));
                                    }
                                    sw.WriteLine(strBtchHeader);
                                }
                                if (strReadLine.StartsWith("6"))
                                {
                                    strEntryDetail = strReadLine;
                                    if (i == 0)
                                    {
                                        strTraceNo = strEntryDetail.Substring(87, 7);
                                        EntryCount = Convert.ToInt16(strTraceNo);
                                    }
                                    if (i > 0)
                                    {
                                        EntryCount++;
                                        strEntryDetail = strEntryDetail.Substring(0, 87) + EntryCount.ToString().PadLeft(7, (char)48);
                                    }
                                    sw.WriteLine(strEntryDetail);
                                }
                                if (strReadLine.StartsWith("8"))
                                {
                                    strBtchCntrl = strReadLine;
                                    if (i > 0)
                                    {
                                        //btchEntry++;
                                        strBtchCntrl = strBtchCntrl.Substring(0, 87) + BatchCnt.ToString().PadLeft(7, (char)48);
                                    }
                                    sw.WriteLine(strBtchCntrl);
                                }
                                if (strReadLine.StartsWith("9"))
                                {
                                    strFileCntrl = strReadLine;

                                    strBtchCnt = strReadLine.Substring(1, 6);
                                    strEntrycnt = strReadLine.Substring(13, 8);
                                    strEntryHash = strReadLine.Substring(21, 10);
                                    strDebitAmnt = strReadLine.Substring(31, 12);
                                    strCreditAmnt = strReadLine.Substring(43, 12);

                                    BtchCnt += Convert.ToDouble(strBtchCnt);
                                    Entrycnt += Convert.ToDouble(strEntrycnt);
                                    EntryHash += Convert.ToDouble(strEntryHash);
                                    DebitAmnt += Convert.ToDouble(strDebitAmnt);
                                    CreditAmnt += Convert.ToDouble(strCreditAmnt);

                                    if (i == lstACH.SelectedItems.Count - 1)
                                    {
                                        strFileCntrl = strFileCntrl.Substring(0, 1) + BtchCnt.ToString().PadLeft(6, (char)48) + strFileCntrl.Substring(7, (strFileCntrl.Length - 7));
                                        strFileCntrl = strFileCntrl.Substring(0, 13) + Entrycnt.ToString().PadLeft(8, (char)48) + strFileCntrl.Substring(21, (strFileCntrl.Length - 21));
                                        strFileCntrl = strFileCntrl.Substring(0, 21) + EntryHash.ToString().PadLeft(10, (char)48) + strFileCntrl.Substring(31, (strFileCntrl.Length - 31));
                                        strFileCntrl = strFileCntrl.Substring(0, 31) + DebitAmnt.ToString().PadLeft(12, (char)48) + strFileCntrl.Substring(43, (strFileCntrl.Length - 43));
                                        strFileCntrl = strFileCntrl.Substring(0, 43) + CreditAmnt.ToString().PadLeft(12, (char)48) + strFileCntrl.Substring(55, (strFileCntrl.Length - 55));
                                        sw.WriteLine(strFileCntrl);
                                    }
                                }
                            }
                        }
                    }
                    if (i == lstACH.SelectedItems.Count - 1)
                    {
                        MessageBox.Show("File Has Been Merged Successfully");
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("One of the Selected File is not a Valid ACH File");
                    break;
                }
            }
        }
    }

Checking for Each and every line Length

 private static bool LinesHaveCorrectLength(string[] lines, int expectedLineLength)
    {
        foreach (string item in lines)
        {
            if (item.Length != expectedLineLength)
            {
                return false;
            }
        }
        return true;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:40:35+00:00Added an answer on May 20, 2026 at 11:40 pm

    Just check them all first – and if all good, THEN begin merge.

            if (lstACH.SelectedIndices.Count != 2)
            {
                MessageBox.Show("Select 2 Files To Merge");
                return;
            }
    
            foreach (String fileName in lstACH.SelectedItems)
            {
                if( LinesHaveCorrectLength( File.ReadAllLines(fileName), 94 ) == false )
                {
                    MessageBox.Show("File: " + fileName + " has an incorrect line length");
                    return;
                }
            }
    
            // Now process them all again to merge:
            foreach(String fileName in lstACH.SelectedItems)
            {
                // ... do merge logic
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all, I'm having a difficult time wording this properly which is why im
I have a MS-Visual Studio 2005 workspace having all c code. This application(exe) allocates
Hi All I am currently having an issue calling a WCF service from a
Having been given the all clear to move code from my Page_Loaded method to
I seem to be having issues with the selected index on a list box.
I'm making a Windows Form project that will search for files in the specified
Friends text box with autocomplete script (which supports multiple word completion from userstable fullname
Following on from this answer , I am having a spot of bother with
I've been having a problem with making sorted lists from dictionaries. I have this
I'm having a bit of trouble populating a listbox on a windows form from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.