将数据从文本框传输到流读取器

发布于 2024-10-30 23:12:43 字数 1162 浏览 0 评论 0原文

我是 c# 新手 在此之前,我尝试让流读取器读取文本框中的结束数据。 现在...我尝试将数据从文本框逐行移动到另一个流读取器 我尝试这个代码

String filename = ContentBox.Text;

        if (File.Exists(filename))
        {
            using (StreamReader fileReader = new StreamReader(filename))
            {

                String fileRow = "";

                while ((fileRow = fileReader.ReadLine()) != null)
                {

                    String[] fileDataField = fileRow.Split(new string[] { "\r\n\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                    String ListSplitLineByLine = "";

                    //line = line.Replace("\r\n", " ");
                    //String[] SplitItemLineByLine1 = (line).Split(' ');


                    foreach (string lineByLine in fileDataField)
                    {
                        ListSplitLineByLine += "\r\n" + lineByLine;
                    }

                    txtCaseInputs.Text = ListSplitLineByLine.Trim();

                    GenCombItems();

                }
            //Close the StreamReader
            fileReader.Close();
            }

       }    

这没有错误,但是当我运行时没有任何错误...... 是我的编码错误吗?

i'm new with c#
before this i try that streamreader read to end data in textbox.
now...i'm try to move data from textbox to another streamreader line by line
i try this code

String filename = ContentBox.Text;

        if (File.Exists(filename))
        {
            using (StreamReader fileReader = new StreamReader(filename))
            {

                String fileRow = "";

                while ((fileRow = fileReader.ReadLine()) != null)
                {

                    String[] fileDataField = fileRow.Split(new string[] { "\r\n\t", " " }, StringSplitOptions.RemoveEmptyEntries);
                    String ListSplitLineByLine = "";

                    //line = line.Replace("\r\n", " ");
                    //String[] SplitItemLineByLine1 = (line).Split(' ');


                    foreach (string lineByLine in fileDataField)
                    {
                        ListSplitLineByLine += "\r\n" + lineByLine;
                    }

                    txtCaseInputs.Text = ListSplitLineByLine.Trim();

                    GenCombItems();

                }
            //Close the StreamReader
            fileReader.Close();
            }

       }    

it is no error but when i run there is nothing...
is it my coding error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-11-06 23:12:43

在每次迭代中,您将该迭代的结果分配给 txtCaseInputs.Text,“覆盖”上一次迭代的结果。

如果您希望文本框包含文件中所有字段的串联,则应在每次迭代中附加而不是分配:

txtCaseInputs.Text += ListSplitLineByLine.Trim();

并且不要忘记在开始整个过程​​之前清除文本框的内容。

On each iteration, you assign that iteration's result to txtCaseInputs.Text, writing "over" the previous iteration's result.

If you want the textbox to contain concatenation of all fields from the file, you should append on each iteration instead of assigning:

txtCaseInputs.Text += ListSplitLineByLine.Trim();

And don't forget to clear the textbox's contents before starting the whole process.

一抹微笑 2024-11-06 23:12:43

在那里放置一些调试语句,直到它正常工作为止。

您稍后可以随时删除它们。

public string ZTest(string contentBoxText) {
  string retValue = null;
  string filename = contentBoxText;
  if (File.Exists(filename)) {
    using (StreamReader fileReader = new StreamReader(filename)) {
      string fileRow;
      while ((fileRow = fileReader.ReadLine()) != null) {
        string[] fileDataField = fileRow.Split(new string[] { "\r\n\t", " " }, StringSplitOptions.RemoveEmptyEntries);
        string ListSplitLineByLine = "";
        foreach (string lineByLine in fileDataField) {
          ListSplitLineByLine += "\r\n" + lineByLine;
        }
        if (!String.IsNullOrEmpty(ListSplitLineByLine)) {
          retValue = ListSplitLineByLine.Trim();
          GenCombItems();
        } else {
          MessageBox.Show("No data");
        }
      }
      // Line below is not necessary. Handled by the "using" clause.
      // fileReader.Close();
    }
  } else {
    MessageBox.Show("File Not Found");
  }
  return retValue;
}

Put a few debug statements in there until you get it working right.

You can always remove them later.

public string ZTest(string contentBoxText) {
  string retValue = null;
  string filename = contentBoxText;
  if (File.Exists(filename)) {
    using (StreamReader fileReader = new StreamReader(filename)) {
      string fileRow;
      while ((fileRow = fileReader.ReadLine()) != null) {
        string[] fileDataField = fileRow.Split(new string[] { "\r\n\t", " " }, StringSplitOptions.RemoveEmptyEntries);
        string ListSplitLineByLine = "";
        foreach (string lineByLine in fileDataField) {
          ListSplitLineByLine += "\r\n" + lineByLine;
        }
        if (!String.IsNullOrEmpty(ListSplitLineByLine)) {
          retValue = ListSplitLineByLine.Trim();
          GenCombItems();
        } else {
          MessageBox.Show("No data");
        }
      }
      // Line below is not necessary. Handled by the "using" clause.
      // fileReader.Close();
    }
  } else {
    MessageBox.Show("File Not Found");
  }
  return retValue;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文