首页 > 解决方案 > C#如何通过line#将openFileDialog中的txt文件放入数组scriptFile[][]中,以及行字符串内容

问题描述

我正在尝试在 C# 中创建一个工具。该工具允许用户逐行查看一些外语文本,并在下面的文本框中输入他们的人工翻译,提交并最终保存到新的文本文件中。

我正在尝试使用 openFileDialog 打开一个 .txt 文件,然后通过 for 循环逐行发送,该循环将添加到二维数组中,有 4 件事:

Things we need:
        Array scriptFile[][]
            scriptFile[X][0] = Int holding the Line number
            scriptFile[X][1] = First line piece
            scriptFile[X][2] = Untranslated Text
            scriptFile[X][3] = Translated Text input

Array 的第一部分是 Integer 中的行号。第二段和第三段是由 TAB 分隔的 2 段文本。

Example Text File:
Dog 슈퍼 지방입니다.
cat 일요일에 빨간색입니다.
Elephant    적의 피로 위안을 찾는다.
Mouse   그의 백성의 죽음을 복수하기 위해 싸우십시오.
racoon  즉시 의료 지원이 필요합니다.

然后:

So array: 
scriptFile[0][0] = 1
scriptFile[0][1] = Dog
scriptFile[0][2] = 슈퍼 지방입니다.
scriptFile[0][3] = "" (Later input as human translation)

如果我能破解这个,那么其他一切都会很快到位。我正在继续寻找解决方案,但我对 C# 的了解有限,因为我主要是 Java/PHP 人:/

到目前为止,我的留置权倒计时,并继续致力于将所有内容排序到一个数组中。到目前为止,我有什么:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.continueButton.Click += new System.EventHandler(this.continueButton_Click);
        }
        private void continueButton_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(openFile.FileName);
                var lineCount = File.ReadLines(openFile.FileName).Count();
                MessageBox.Show(lineCount.ToString());
                sr.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            openFile.Filter = "Text files (.txt)|*.txt";
        }
    }

标签: c#arraysopenfiledialog

解决方案


可能没有学习效果,但我想通了。

注意这是非常原始的代码。没有异常处理。

using System;
using System.IO;
using System.Threading.Tasks;  
using System.Windows.Forms;

namespace TextArrayManipulation
{ 
    public partial class Form1 : Form
    {
        private TaskCompletionSource<string> _translationSubmittedSource = new TaskCompletionSource<string>();

        private string[,] _result;

        public Form1()
        {
            InitializeComponent();
        }

        private async void buttonOpen_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                if (ofd.ShowDialog() != DialogResult.OK) return;

                _result = new string[File.ReadLines(openFile.FileName).Count(), 4];

                using (var streamReader = new StreamReader(ofd.FileName))
                {
                    var line = string.Empty;
                    var lineCount = 0;
                    while (line != null)
                    {
                        line = streamReader.ReadLine();

                        if (string.IsNullOrWhiteSpace(line)) continue;

                        // update GUI
                        textBoxLine.Text = line;
                        labelLineNumber.Text = lineCount.ToString();

                        // wait for user to enter a translation
                        var translation = await _translationSubmittedSource.Task;

                        // split line at tabstop
                        var parts = line.Split('\t');
                        // populate result
                        _result[lineCount, 0] = lineCount.ToString();
                        _result[lineCount, 1] = parts[0];
                        _result[lineCount, 2] = parts[1];
                        _result[lineCount, 3] = translation;

                        // reset soruce
                        _translationSubmittedSource = new TaskCompletionSource<string>();
                        // clear TextBox
                        textBoxTranslation.Clear();
                        // increase line count
                        lineCount++;
                    }
                }
                // proceede as you wish...
            }
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            _translationSubmittedSource.TrySetResult(textBoxTranslation.Text);
        }
    }
}

推荐阅读