我正在使用C#应用程序读取文件并使用空格将数据解析为列表或数组。从文件传入的数据是以列的形式,因此数组或列表需要用空格分隔,以便我可以访问它进行操作。
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
var files = new[] { nailAssignmentResult, nailFixtureResult, nailContactResult };
foreach(var file in files)
{
// parse the contents into a list separated by a space
List<string> lines = File.ReadAllLines(file).ToList();
foreach (var line in lines)
{
// for each line of data, what needs to be compared
if (line.StartsWith("*"))
{
continue;
}
}
}
}
// 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;
}
}
}
}
我不确定我是否应该将数据读入列表或数组,因为它需要用空格分隔,我将比较文本文件中的数据。我有点卡住了,因为我知道我需要做什么前进,只是不知道如何做到这一点。
1条答案
按热度按时间7jmck4yq1#
我知道你最终需要处理一个单元格值,这样你就可以避免将它存储在List/Array中。
转换为一个列表,然后需要再次进一步处理以获得单个值,其成本大致相同。
由于File.ReadAllLines(file)已经为您提供了包含所有行的string[ ](假设它们在带有回车符的文本文件中很好地区分开来),您可以直接循环这些行并拆分每一行以处理数据元素或单元格值。
我希望这为您的问题提供了另一种方法。