尝试将文件发送到C#中的另一个类 [英] Trying to send a file to another class in C#
问题描述
我试图将流阅读器获取的文件发送到另一个类,以便该类可以从中提取信息。我尝试将其提取到表单中,但这会使它产生混乱,并且我肯定这是一种糟糕的方法。有人可以建议一种方法吗?
as the title suggests im trying to send a file acquired by stream reader to another class so that that class can extract the information from it. I have tried extracting it within the form but this makes it confusing and im certain is a poor way of doing it. Can anyone suggest a way?
这是标识文件的类。
namespace DistanceEstimatorFinal
{
public partial class Form1 : Form
private void openDataListToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "CSV files (*.csv)|*.csv|Text files ( *.txt)|*.txt |All files (*.*)|*.*";
if (ofd.ShowDialog(this).Equals(DialogResult.OK))
{
Stream fileStream = ofd.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
}
}
}
现在我需要某种方式将其发送到这里...我看不到如何:[
Now I need some way of sending it here... I can't see how :[
namespace DistanceEstimatorFinal
{
public class dataPoints
{
List<dataPoint> Points;
public dataPoints( )
{
Points = new List<dataPoint>();
TextReader tr = new StreamReader();
string input;
while ((input = tr.ReadLine()) != null)
{
string[] bits = input.Split(',');
dataPoint a = new dataPoint(bits[0],bits[1],bits[2]);
Points.Add(a);
}
tr.Close();
}
internal dataPoint getItem(int p)
{
if (p < Points.Count)
{
return Points[p];
}
else
return null;
}
}
}
任何帮助
推荐答案
我只是将文件的路径传递给您的类,然后打开文件进行读取。 / p>
I would just pass the path of the file to your class and then open the file for reading.
if (ofd.ShowDialog(this).Equals(DialogResult.OK))
{
var path = ofd.FileName;
//Pass the path to the dataPoints class and open the file in that class.
}
您可以在类的构造函数或方法本身中传递路径作为参数。
You could pass the path in the constructor of the class or to the method itself as a parameter.
这篇关于尝试将文件发送到C#中的另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!