C# 访问多个文件
我的程序没有在文件中循环。程序将所有文件名正确写入“文件列表”。然后我希望它输出每个文件中数据列表的平均值、最小值和最大值。但是,无论何时运行,它都只输出一个文件的平均值、最小值和最大值。我认为这种循环运行得不太好 我尝试在textwriter tw3和它的右括号之间创建额外的循环,但这不起作用。我也尝试了同样的tw4,但还是不起作用。我不确定问题是否是由于循环造成的,或者我是否没有使用正确的语法调用文件夹中的每个文件。整个代码如下C# 访问多个文件,c#,C#,我的程序没有在文件中循环。程序将所有文件名正确写入“文件列表”。然后我希望它输出每个文件中数据列表的平均值、最小值和最大值。但是,无论何时运行,它都只输出一个文件的平均值、最小值和最大值。我认为这种循环运行得不太好 我尝试在textwriter tw3和它的右括号之间创建额外的循环,但这不起作用。我也尝试了同样的tw4,但还是不起作用。我不确定问题是否是由于循环造成的,或者我是否没有使用正确的语法调用文件夹中的每个文件。整个代码如下 namespace Filereader_m_15062012
namespace Filereader_m_15062012
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] fileEntries;
private void Form1_Load(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
// create a list to insert the data into
//put all the files in the root directory into array
string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");
// Display all files.
TextWriter tw1 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/listoffiles.txt");
List<string> filenames = new List<string>();
tw1.WriteLine("--- Files: ---");
foreach (string path in fileEntries)
{
tw1.WriteLine(path);
}
tw1.Close();
TextWriter tw2 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/errorlist.txt");
foreach (string path in fileEntries)
{
string text = "";
// create a list to insert the data into
List<float> noise = new List<float>();
TextWriter tw3 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/rawdata.txt");
string file = path;
FileInfo src = new FileInfo(file);
TextReader reader = src.OpenText();
text = reader.ReadLine();
// while the text being read in from reader.Readline() is not null
while (text != null)
{
text = reader.ReadLine();
{
while (text != null)
{
text = reader.ReadLine();
if (text != null)
{
string[] words = text.Split(',');
noise.Add(Convert.ToSingle(words[3]));
// write text to a file
tw3.WriteLine(text);
//foreach (string word in words)
//{
// tw.WriteLine(word);
//}
}
}
}
tw3.Close();
int count = 0;
float sum = 0;
float mean = 0;
float max = 0;
float min = 100;
List<string> means = new List<string>();
List<string> maximums = new List<string>();
List<string> minimums = new List<string>();
TextWriter tw4 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/noise.txt");
foreach (float ns in noise)
{
tw4.WriteLine(Convert.ToString(ns));
count++;
sum += ns;
mean = sum / count;
float min1 = 0;
if (ns > max)
max = ns;
else if (ns < max)
min1 = ns;
if (min1 < min && min1 > 0)
min = min1;
else
min = min;
}
means.Add(Convert.ToString(mean));
maximums.Add(Convert.ToString(max));
minimums.Add(Convert.ToString(min));
TextWriter tw5 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymeans.txt");
tw5.WriteLine("Mean Noise");
tw5.WriteLine("==========");
foreach (string m in means)
{
tw5.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw5.Close();
TextWriter tw6 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymaximums.txt");
tw6.WriteLine("Maximum Noise");
tw6.WriteLine("=============");
foreach (string m in maximums)
{
tw6.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw6.Close();
TextWriter tw7 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summaryminimums.txt");
tw7.WriteLine("Minimum Noise");
tw7.WriteLine("=============");
foreach (string m in maximums)
{
tw7.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw7.Close();
tw4.Close();
}
tw2.Close();
}
}
private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{
}
}
}
namespace文件读取器_m_15062012
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
字符串[]文件项;
私有void Form1\u加载(对象发送方、事件参数e)
{
DialogResult=folderBrowserDialog1.ShowDialog();//显示对话框。
//创建一个列表以将数据插入其中
//将根目录中的所有文件放入数组
string[]fileEntries=Directory.GetFiles(folderBrowserDialog1.SelectedPath,“*.csv”);
//显示所有文件。
TextWriter tw1=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/listoffiles.txt”);
列表文件名=新列表();
tw1.WriteLine(“--Files:--”);
foreach(文件项中的字符串路径)
{
tw1.写线(路径);
}
tw1.Close();
TextWriter tw2=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/errorlist.txt”);
foreach(文件项中的字符串路径)
{
字符串文本=”;
//创建一个列表以将数据插入其中
列表噪声=新列表();
TextWriter tw3=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/rawdata.txt”);
字符串文件=路径;
FileInfo src=新的FileInfo(文件);
TextReader=src.OpenText();
text=reader.ReadLine();
//从reader.Readline()读入的文本不为空
while(text!=null)
{
text=reader.ReadLine();
{
while(text!=null)
{
text=reader.ReadLine();
如果(文本!=null)
{
string[]words=text.Split(',');
noise.Add(转换为single(单词[3]);
//将文本写入文件
tw3.文字线(文本);
//foreach(单词中的字符串)
//{
//tw.WriteLine(单词);
//}
}
}
}
tw3.Close();
整数计数=0;
浮点数和=0;
浮动平均值=0;
浮动最大值=0;
浮动最小值=100;
列表意味着=新列表();
列表最大值=新列表();
列表最小值=新列表();
TextWriter tw4=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/noise.txt”);
foreach(噪声中的浮子)
{
tw4.WriteLine(Convert.ToString(ns));
计数++;
总和+=ns;
平均值=总和/计数;
float min1=0;
如果(ns>max)
max=ns;
否则如果(ns0)
min=min1;
其他的
min=min;
}
平均值。添加(转换为字符串(平均值));
添加最大值(转换为字符串(最大值));
最小值。添加(转换为字符串(最小值));
TextWriter tw5=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/summarymeans.txt”);
tw5.写入线(“平均噪声”);
tw5.写线(“================”);
foreach(字符串m的平均值)
{
tw5.WriteLine(“mote_noise:{0}”,Convert.ToString(m));
}
tw5.Close();
TextWriter tw6=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/summarymaximums.txt”);
tw6.写入线(“最大噪声”);
tw6.写线(“======================”);
foreach(最大值为m的字符串)
{
tw6.WriteLine(“mote_noise:{0}”,Convert.ToString(m));
}
tw6.Close();
TextWriter tw7=新的StreamWriter(folderBrowserDialog1.SelectedPath+“/summaryminimums.txt”);
tw7.写入线(“最小噪声”);
tw7.写线(“======================”);
foreach(最大值为m的字符串)
foreach (string path in fileEntries)
{
...
string text = reader.ReadLine();
// while the text being read in from reader.Readline() is not null
while (text != null)
{
string[] words = text.Split(',');
noise.Add(Convert.ToSingle(words[3]));
// write text to a file
tw3.WriteLine(text);
//foreach (string word in words)
//{
// tw.WriteLine(word);
//}
...
text = reader.ReadLine();
}
tw3.Close();
}
// while the text being read in from reader.Readline() is not null
while (text != null)
{
string[] words = text.Split(',');
noise.Add(Convert.ToSingle(words[3]));
// write text to a file
tw3.WriteLine(text);
text = reader.ReadLine();
}
foreach (float ns in noise)
{
tw4.WriteLine(Convert.ToString(ns));
count++;
sum += ns;
mean = sum / count;
float min1 = 0;
if (ns > max)
max = ns;
else if (ns < max)
min1 = ns;
if (min1 < min && min1 > 0)
min = min1;
means.Add(Convert.ToString(mean));
maximums.Add(Convert.ToString(max));
minimums.Add(Convert.ToString(min));
}
private void Form1_Load(object sender, EventArgs e)
DialogResult result = folderBrowserDialog1.ShowDialog(); // Show the dialog.
// create a list to insert the data into
//put all the files in the root directory into array
string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.csv");
// Display all files.
TextWriter tw1 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/listoffiles.txt");
List<string> filenames = new List<string>();
tw1.WriteLine("--- Files: ---");
foreach (string path in fileEntries)
{
tw1.WriteLine(path);
}
tw1.Close();
TextWriter tw2 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/errorlist.txt");
foreach (string path in fileEntries)
{
string text = "";
// create a list to insert the data into
List<float> noise = new List<float>();
TextWriter tw3 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/rawdata.txt");
string file = path;
FileInfo src = new FileInfo(file);
TextReader reader = src.OpenText();
text = reader.ReadLine();
// while the text being read in from reader.Readline() is not null
while (text != null)
{
string[] words = text.Split(',');
noise.Add(Convert.ToSingle(words[3]));
// write text to a file
tw3.WriteLine(text);
text = reader.ReadLine();
}
tw3.Close();
int count = 0;
float sum = 0;
float mean = 0;
float max = 0;
float min = 100;
List<string> means = new List<string>();
List<string> maximums = new List<string>();
List<string> minimums = new List<string>();
TextWriter tw4 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/noise.txt");
foreach (float ns in noise)
{
tw4.WriteLine(Convert.ToString(ns));
count++;
sum += ns;
mean = sum / count;
float min1 = 0;
if (ns > max)
max = ns;
else if (ns < max)
min1 = ns;
if (min1 < min && min1 > 0)
min = min1;
means.Add(Convert.ToString(mean));
maximums.Add(Convert.ToString(max));
minimums.Add(Convert.ToString(min));
}
TextWriter tw5 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymeans.txt");
tw5.WriteLine("Mean Noise");
tw5.WriteLine("==========");
foreach (string m in means)
{
tw5.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw5.Close();
TextWriter tw6 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summarymaximums.txt");
tw6.WriteLine("Maximum Noise");
tw6.WriteLine("=============");
foreach (string m in maximums)
{
tw6.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw6.Close();
TextWriter tw7 = new StreamWriter(folderBrowserDialog1.SelectedPath + "/summaryminimums.txt");
tw7.WriteLine("Minimum Noise");
tw7.WriteLine("=============");
foreach (string m in maximums)
{
tw7.WriteLine("mote_noise: {0}", Convert.ToString(m));
}
tw7.Close();
tw4.Close();
}
tw2.Close();
}