正如标题所示,我试图将流阅读器获取的文件发送到另一个类,以便该类可以从中提取信息.我已经尝试在表单中提取它但这使得它令人困惑,并且我确定这是一种糟糕的方式.谁能提出建议?
这是标识文件的类..
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))
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要一些方法将它发送到这里......我看不出如何:[
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激
我只是将文件的路径传递给您的类,然后打开文件进行读取.
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.
}
Run Code Online (Sandbox Code Playgroud)
您可以在类的构造函数中传递路径,也可以将方法本身作为参数传递.
| 归档时间: |
|
| 查看次数: |
484 次 |
| 最近记录: |