winforms 循环访问C#应用程序读入的文件

fcg9iug3  于 2023-05-01  发布在  C#
关注(0)|答案(2)|浏览(147)

我正在用C#构建一个Winforms应用程序,我将阅读3个文件,然后迭代它们以执行一些功能来验证它们。我无法迭代,似乎无法弄清楚,因为我正在学习Winforms和C#。

// store each file path into a string
string nailAssignmentResult = textBox1.Text;
string nailFixtureResult = textBox2.Text;
string nailContactResult = textBox3.Text;

上面的声明将3个文本框中的文件路径存储到字符串变量中,以便我进行迭代。
我已经尝试了foreach循环和for循环,但我似乎不知道如何迭代它们。我想把每个文件中的信息解析成一个由空格分隔的列表。

namespace FixtureReports
{
    public partial class btnFileSelect : Form
    {
        // contructor
        public btnFileSelect()
        {
            InitializeComponent();
        }

        // this will do the verification 
        private void btnVerify_Click(object sender, EventArgs e)
        {
            // clear textbox each time its clicked
            results.Clear();

            //store each file path into a string
            string nailAssignmentResult = textBox1.Text;
            string nailFixtureResult = textBox2.Text;
            string nailContactResult = textBox3.Text;

            // error for if all three paths are empty 
            TextBox errorMessage = results;
            if (nailAssignmentResult == "" && nailFixtureResult == "" && nailContactResult == "")
                errorMessage.Text = "ERROR: Cannot have empty path for all three reports!!";

            // verifying each file path exists
            if (File.Exists(nailAssignmentResult))
                errorMessage.AppendText("Nail Assignment Report Found");

            if (File.Exists(nailFixtureResult))
                errorMessage.AppendText("\r\nNail Fixture Report Found");

            if (File.Exists(nailContactResult))
                errorMessage.AppendText("\r\nNail Contact List Report Found");

            // grab each file and iterate through each of them 

            // parse the contents into a list separated by a space horizontally

            // for each line of data, what needs to be compared

            // take in receiver pin and plug into brians function to get pylon

            // compare to existing pylon designation in report and see if it matches

            // have a counter for how many were correct 

            // if incorrect, display pylon blocks and receiver pins
        }

        // prompt for file selection
        private void nailFixtureBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog.FileName;
            }
        }

        // prompt for file selection
        private void nailAssignmentBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog.FileName;
            }
        }

        // prompt for file selection
        private void nailContactBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = openFileDialog.FileName;
            }
        }
    }
}
pbwdgjma

pbwdgjma1#

为了“迭代”,你需要一个集合。你可以从这三个变量创建一个简单的数组:

var files = new[] {nailAssignmentResult, nailFixtureResult, nailContactResult};

并迭代 that,但是如果你在一个函数中有“处理”,你可以只传入文件名(以及任何其他需要的信息),那么你不需要迭代:

var assignmentResults = ProcessFile(nailAssignmentResult);
var fixtureResults = ProcessFile(nailFixtureResult);
var contractResults = ProcessFile(nailContactResult);

这只是两个选项--有很多方法可以做到这一点,包括创建一个迭代器函数,使用yield return返回每个值,但我认为没有必要使用任何比简单的值数组更花哨的东西。

zlhcx6iw

zlhcx6iw2#

从这个开始:

foreach( var line in File.ReadLines(nailAssignmentResult))
{
    // ...
}

在深入研究之前,您需要编写更多自己的代码,并明确规范。
另外:* 不要检查File.Exists() *!存在只是您可能无法打开文件的几个原因之一。此外,文件系统是volatile的,这意味着文件的状态可能会在您检查它和尝试打开它之间的短时间内发生变化。因此,您必须能够在尝试打开文件时处理异常,而不考虑之前的任何检查。
因此,Exists()检查实际上只是一个额外的不必要(浪费)的I/O操作,磁盘I/O是您在一台计算机中可以做的最慢的事情之一。在几乎所有情况下,最好将精力完全投入到异常处理程序中。

相关问题