用不同的方法读取相同的文本文件

时间:2015-05-13 20:44:24

标签: c# winforms text-files readfile

BtnNext_Click方法中,它只是从文本文件中读取,就像它是一个不同的文本文件,而不是已经打开的文本文件。它不是逐行的。 我需要帮助

以下是代码:

public void ScrubData()
{           
    string FileName1;
    string FilePath1;

    // Display an OpenFile Dialog box for user
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "txt Files|*.txt";
    openFileDialog1.Title = "Select a txt File";

    // Show the Dialog. If user clicked OK in the dialog
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        try
        {
            String strFileName = openFileDialog1.FileName;
            String strFilePath = System.IO.Path.GetDirectoryName(strFileName);
            String fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);
            String strFileNameAndPathNew = strFilePath +
                openFileDialog1.InitialDirectory + "\\" + fileName + "_scrubbed.txt";

            // If scrubbed file exists, delete it first
            if (System.IO.File.Exists(strFileNameAndPathNew))
            {
                System.IO.File.Delete(strFileNameAndPathNew);
            } // End IF
            if (System.IO.File.Exists(strFileName))
            {
                int lineCount = System.IO.File.ReadAllLines(strFileName).Length;
                System.IO.StreamReader file = new System.IO.StreamReader(strFileName);
                // Status label 
                LblStatus.Text = "File Loaded Successfully";
                LblStatus.Visible = true;
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    const char DELIM = '|';
                    // MessageBox.Show(line);

                    string[] word = line.Split(DELIM);
                     Txt2NormAccNum.Text = word[3];
                    //string accScrubbed = ReplaceData(word[0],"SSN");
                    Txt3NormAmnt.Text = word[4];
                    Txt4NormFirstNam.Text = word[1];
                    Txt5NormLastNam.Text = word[2];
                    Txt6NormSS.Text = word[0];
                    Txt7NormItem.Text = word[5];
                } // End WHILE
            } // End IF
            else
            {
                // Status label 
                LblStatus.Text = "File Load Failed!";
                LblStatus.Visible = true;
            } // End ELSE
            // Text box one code:
            FileName1 = openFileDialog1.FileName;
            Txt1.Text = FileName1;
            //
        } // End TRY
        catch (Exception e1)
        {
             if (e1.Source != null)
            {
                Console.WriteLine("IOException source: {0}", e1.Source);
                throw;
            } // End IF
        } // End CATCH
    } // End IF
} // End Scrub Method

我需要在下一个方法中重用诸如“strFileName”之类的变量。

我正在创建一个先前的&下一个按钮循环文本文件中的每一行:

public void BtnNext_Click(object sender, EventArgs e)
{
    StreamReader myReader2 = new StreamReader("colin.txt");
    string line2 = "";

    while (line2 != null)
    {
        line2 = myReader2.ReadLine();
        if (line2 != null)
        {
            const char DELIM = '|';
            // MessageBox.Show(line);
            string[] word = line2.Split(DELIM);
            Txt2NormAccNum.Text = word[3];
            Txt3NormAmnt.Text = word[4];
            Txt4NormFirstNam.Text = word[1];
            Txt5NormLastNam.Text = word[2];
            Txt6NormSS.Text = word[0];
            Txt7NormItem.Text = word[5];

            //Txt12ScrubSS.Text;
            //Txt10ScrubFirstNam.Text;
            //Txt11ScrubLastNam.Text;
            //Txt8ScrubAcctNum.Text;
            //Txt9ScrubAmt.Text;
            //Txt13ScrubItem.Text;
        }
    }
    myReader2.Close();

} // end method

如果你看到我在说什么:设计是:用户打开文件,文件的第一行文字显示在表格上,然后我有一个'上一个'和'下一个'按钮,我想循环通过来自同一档案的文字。

是的,我想我知道你的意思在这里看看:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;

    namespace Project2_DataScrubber
    {
public partial class Form1 : Form
{ // Begin Class #1

    public Form1()
    { // Begin Main Method


        InitializeComponent();

        // MessageBox.Show(GetRandomNumbers().ToString());


    } // End Main Method

    private void Btn4_Click(object sender, EventArgs e)
    { // Btn4 CLICK Method

        // Closes Form 1
        this.Close();
    } // End Method

    private void Btn3_Click(object sender, EventArgs e)
    { // Btn3 CLICK Method

        // Display alert message box of are you sure you want to reset the data
        DialogResult dialogResult1 = MessageBox.Show("Are you want to reset the data?", "ALERT", MessageBoxButtons.YesNo);
        if (dialogResult1 == DialogResult.Yes)
        {
            // Resets all the data, textboxes, ect
            Txt1.Text = "No file loaded.";
            Txt2NormAccNum.Clear();
            Txt3NormAmnt.Clear();
            Txt4NormFirstNam.Clear();
            Txt5NormLastNam.Clear();
            Txt6NormSS.Clear();
            Txt7NormItem.Clear();
            Txt8ScrubAcctNum.Clear();
            Txt9ScrubAmt.Clear();
            Txt10ScrubFirstNam.Clear();
            Txt11ScrubLastNam.Clear();
            Txt12ScrubSS.Clear();
            Txt13ScrubItem.Clear();
            Txt14ScrubYesNo.Clear();
            LblStatus.Visible = false;
        }
        else if (dialogResult1 == DialogResult.No)
        {
            // Do nothing
        }

    } // End Method

    public  void Btn1_Click(object sender, EventArgs e)
    { // Btn1 CLICK Method

        ScrubData();
    } // End Method

    public void Btn2_Click (object sender, EventArgs e)
    {
        if (Txt2NormAccNum.Text != "" || Txt3NormAmnt.Text != "" || Txt4NormFirstNam.Text != "" || Txt5NormLastNam.Text != "" ||
            Txt6NormSS.Text != "" || Txt7NormItem.Text != "")
        {
                                                                //
            Txt12ScrubSS.Text = GetRandomNumbers().ToString();  // Replace SS textbox
            //
            #region
            int lastNameLetters = Txt5NormLastNam.Text.Length;  //   
            string lettersTwo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            Random randLetters = new Random();
            string randomString = "";
            for (int i = 0; i < lastNameLetters; i++)
            {                                                   // Replace Last Name
                randomString += lettersTwo
                    [randLetters.Next(0, 25)].ToString();
            }
            Txt11ScrubLastNam.Text = randomString;              //
            #endregion
            #region
            var newAccountNum = "";                             //
            int numOfCharacters = 4; // # to leave behind
            for (var i = 0; i<Txt2NormAccNum.Text.Length - numOfCharacters; i++)
            {
                newAccountNum += "X";                           // Replace Account Number
            }
            newAccountNum += Txt2NormAccNum.Text.Substring
                (Txt2NormAccNum.Text.Length - numOfCharacters);
            Txt8ScrubAcctNum.Text = newAccountNum;              //
            #endregion
            #region
            double moneyAmountDoub = 0;                         //
            string moneyAmountStr = "";
            moneyAmountStr = Txt3NormAmnt.Text;
            moneyAmountDoub = Convert.ToDouble(moneyAmountStr);
            if (moneyAmountDoub > 100.00)
            {                                                   // Get Yes or No answer
                Txt14ScrubYesNo.Text = "Yes";               
            }
            else
            {
                Txt14ScrubYesNo.Text = "No";
            }                                                   //
            #endregion

            Txt10ScrubFirstNam.Text = Txt4NormFirstNam.Text;
            Txt13ScrubItem.Text = Txt7NormItem.Text;
            Txt13ScrubItem.Text = Txt7NormItem.Text;
            Txt9ScrubAmt.Text = Txt3NormAmnt.Text;




        }
        else 
        {
            MessageBox.Show("Error: Information missing from the Data section");
        }
    }

    public void ScrubData()
    { // Begin Scrub Method

2 个答案:

答案 0 :(得分:1)

当然它不是逐行的。每次单击按钮,您只需阅读第一行。

最好的方法是将文件中的所有行都读入List<string>,然后使用int counter递增或递减,具体取决于它们是向前还是向前移动。只需每次递增/递减,并将文本设置为yourListVar[counter]

或者,如果您希望每次都可以在文件中读取ReadLine(),直到您点击与计数器匹配的索引。

答案 1 :(得分:0)

您需要做几件事。首先,您需要创建类级变量(更恰当地称为字段)来保存需要通过不同方法访问的信息。

其次,您需要跟踪文件中的位置(哪一行),因为每次创建StreamReader时,它都会将阅读器放在第一行。作为推论,正如RogueBukkitDev所说,你应该读取文件一次并将其转储到List<string>或数组(string[])中。然后,您可以根据用户的指示 - 前进或后退来增加或减少集合中的当前位置。

看起来像这样:

public partial class Form1 : Form
{ 

    // Class level fields
    private string fileName = String.Empty;
    private string[] fileLines;
    private int currentLine = 0;
    const char DELIM = '|';

    public void ScrubData()
    {           

        // Display an OpenFile Dialog box for user
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "txt Files|*.txt";
        openFileDialog1.Title = "Select a txt File";

        // Show the Dialog. If user clicked OK in the dialog
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            try
            {
                fileName = openFileDialog1.FileName;
                newFileName = String.Format(@"{0}\{1}_scrubed.txt",
                           openFileDialog1.InitialDirectory,
                           Path.GetFileNameWithoutExtension(fileName));
                fileLines = File.ReadAllLines(fileName);
                currentLine = 0;
            } 
        }
    }

    public void BtnNext_Click(object sender, EventArgs e)
    {

        if (currentLine <= fileLines.Length)
        {
            string line2 = fileLines[currentLine];
            string[] word = line2.Split(DELIM);

            Txt2NormAccNum.Text = word[3];
            Txt3NormAmnt.Text = word[4];
            Txt4NormFirstNam.Text = word[1];
            Txt5NormLastNam.Text = word[2];
            Txt6NormSS.Text = word[0];
            Txt7NormItem.Text = word[5];

            currentLine = currentLine + 1;
        }
    }
}

这里有很多变化,我省略了与示例无关的代码。

首先,我声明了4个字段 - 一个用于fileName,一个用于清理文件名,一个用于文件的当前行,一个用于分隔符。

接下来,在ScrubData方法中,用户选择了文件,它只调用File.ReadAllLines(),它将返回一个字符串数组(每行一个数组元素),并将其存储在类级别字段fileLines。它还将currentLine设置为0,以防用户在退出程序之前选择其他文件。

btnNext_Click事件处理程序中,它从行数组(fileLines)中读取当前行 - 确保您没有索引输出边界错误,然后将它们拆分为分隔符并填充文本框。然后它将currentLine增加一个。

要返回,它基本上是相同的逻辑,除了你减少 currentLine 1(并检查以确保你没有超过0)。< / p>

这是我们一直试图解释的基本概要。您可能需要对其进行修改以满足您的实际需求,但原则保持不变。