<i id='MWySf'><tr id='MWySf'><dt id='MWySf'><q id='MWySf'><span id='MWySf'><b id='MWySf'><form id='MWySf'><ins id='MWySf'></ins><ul id='MWySf'></ul><sub id='MWySf'></sub></form><legend id='MWySf'></legend><bdo id='MWySf'><pre id='MWySf'><center id='MWySf'></center></pre></bdo></b><th id='MWySf'></th></span></q></dt></tr></i><div id='MWySf'><tfoot id='MWySf'></tfoot><dl id='MWySf'><fieldset id='MWySf'></fieldset></dl></div>
    1. <tfoot id='MWySf'></tfoot>

    2. <legend id='MWySf'><style id='MWySf'><dir id='MWySf'><q id='MWySf'></q></dir></style></legend>
        <bdo id='MWySf'></bdo><ul id='MWySf'></ul>

        <small id='MWySf'></small><noframes id='MWySf'>

        C# - 将 .txt 文件读入文本框

        时间:2023-06-09
      1. <tfoot id='tvSmd'></tfoot>

          <legend id='tvSmd'><style id='tvSmd'><dir id='tvSmd'><q id='tvSmd'></q></dir></style></legend>
            • <bdo id='tvSmd'></bdo><ul id='tvSmd'></ul>

              <small id='tvSmd'></small><noframes id='tvSmd'>

                <tbody id='tvSmd'></tbody>

                  <i id='tvSmd'><tr id='tvSmd'><dt id='tvSmd'><q id='tvSmd'><span id='tvSmd'><b id='tvSmd'><form id='tvSmd'><ins id='tvSmd'></ins><ul id='tvSmd'></ul><sub id='tvSmd'></sub></form><legend id='tvSmd'></legend><bdo id='tvSmd'><pre id='tvSmd'><center id='tvSmd'></center></pre></bdo></b><th id='tvSmd'></th></span></q></dt></tr></i><div id='tvSmd'><tfoot id='tvSmd'></tfoot><dl id='tvSmd'><fieldset id='tvSmd'></fieldset></dl></div>
                  本文介绍了C# - 将 .txt 文件读入文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用以下代码将 .txt 文件读入多行文本框中.我已经让文件对话框按钮完美工作,但我不确定如何将文件中的实际文本获取到文本框中.这是我的代码.你能帮忙吗?

                  I am trying to read a .txt file into a multi-line text box with the following code. I have gotten the file dialog button to work perfectly, but I am not sure how to get the actual text from the fiile into the textbox. Here is my code. Can you help?

                  private void button_LoadSource_Click(object sender, EventArgs e)
                      {
                          Stream myStream = null;
                          OpenFileDialog openFileDialog1 = new OpenFileDialog();
                  
                          openFileDialog1.InitialDirectory = "c:\";
                          openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                          openFileDialog1.FilterIndex = 2;
                          openFileDialog1.RestoreDirectory = true;
                  
                          if (openFileDialog1.ShowDialog() == DialogResult.OK)
                          {
                              try
                              {
                                  if ((myStream = openFileDialog1.OpenFile()) != null)
                                  {
                                      using (myStream)
                                      {
                                          // Insert code to read the stream here.
                                      }
                                  }
                              }
                              catch (Exception ex)
                              {
                                  MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                              }
                          }
                      }
                  

                  推荐答案

                  如果你只需要完整的文本,你应该使用函数File.ReadAllText - 将对话框中选择的文件名/路径传递给它 (openFileDialog1.FileName).

                  if you just need the complete text, you should use the function File.ReadAllText - pass it the FileName/Path selected in the dialoge (openFileDialog1.FileName).

                  例如要将内容加载到文本框中,您可以编写:

                  to load for example the content into a textbox, you can write:

                   textbox1.Text = File.ReadAllText(openFileDialog1.FileName);
                  

                  打开和使用流有点复杂,你应该查看 using - 语句

                  opening and using streams is a little bit more complicated, for that you should look up the using - statement

                  这篇关于C# - 将 .txt 文件读入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Winform中的空密码字符 下一篇:C# 在禁用的文本框(表单)上显示工具提示

                  相关文章

                1. <i id='yfjGO'><tr id='yfjGO'><dt id='yfjGO'><q id='yfjGO'><span id='yfjGO'><b id='yfjGO'><form id='yfjGO'><ins id='yfjGO'></ins><ul id='yfjGO'></ul><sub id='yfjGO'></sub></form><legend id='yfjGO'></legend><bdo id='yfjGO'><pre id='yfjGO'><center id='yfjGO'></center></pre></bdo></b><th id='yfjGO'></th></span></q></dt></tr></i><div id='yfjGO'><tfoot id='yfjGO'></tfoot><dl id='yfjGO'><fieldset id='yfjGO'></fieldset></dl></div>

                      <legend id='yfjGO'><style id='yfjGO'><dir id='yfjGO'><q id='yfjGO'></q></dir></style></legend>

                      <small id='yfjGO'></small><noframes id='yfjGO'>

                      <tfoot id='yfjGO'></tfoot>
                      • <bdo id='yfjGO'></bdo><ul id='yfjGO'></ul>