开发者

Merge 2 Files and throw an error if an Invalid File selected

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 che开发者_开发知识库cking 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;
    }


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
        }


Ok according to your comment it looks like First you want both files to be validated. if that is the case then:

(There could be many ways , this is one of them)

First define a Function to do the checking for all files:

public bool AreFilesValid(ListBox.SelectedObjectCollection filenames)
{
    int count = filenames.Count;
    bool valid = false;
    for(int i=0;i<count;i++)
    {
        string strFile = filenames[i].ToString();
        string[] lines = File.ReadAllLines(strFile);
        if(LinesHaveCorrectLength(lines, 94)) { valid=true;  }
        else  {  valid = false;   }
    }
    return valid;
}

Then call it in your if condition, i.e just change following lines:

     ...
     strFile = lstACH.SelectedItems[i].ToString();
     lines = File.ReadAllLines(strFile);
     if (LinesHaveCorrectLength(lines, 94))
     {
     ...

To only this:

  ...
  if (AreFilesValid(lstACH.SelectedItems))
  {
  ...

You have already got your else statement down the code to catch when this if condition fails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜