我阅读了文件夹中的文本文件,获得了每一列的绝对值,并在datagrid中显示了最大值和文件名。但是我需要在类中编写方法,并在form.cs中而不是在form.cs中调用它。你能帮助我吗?我的代码是这样的:
DirectoryInfo info = new DirectoryInfo(@"D:\Desktop\..");
FileInfo[] Files = info.GetFiles("*.txt");
List<string> list = new List<string>();
List<Double> values= new List<Double>();
foreach (FileInfo file in Files)
{
string name = info.Name;
list.Add(file.Name);
string[] lines = System.IO.File.ReadAllLines(@"D:\Desktop\..\" + file, Encoding.GetEncoding("windows-1254"));
values.AddRange(MultiColumns(lines));
}
private List<Double> MultiColumns(String[] strs)
{
double col1Max = 0;
double col2Max = 0;
double col3Max = 0;
var list = new List<Double>();
var format = new NumberFormatInfo();
format.NegativeSign = "-";
format.NumberNegativePattern = 1;
format.NumberDecimalSeparator = ".";
foreach (var row in strs)
{
var rowElements = row.Split(',');
Double temp1 = 0;
Double temp2 = 0;
Double temp3 = 0;
Double.TryParse(rowElements[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp1);
Double.TryParse(rowElements[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp2);
Double.TryParse(rowElements[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp3);
col1Max = getMax(col1Max, temp1);
col2Max = getMax(col2Max, temp2);
col3Max = getMax(col3Max, temp3);
}
list.Add(col1Max);
list.Add(col2Max);
list.Add(col3Max);
return list;
}
private double getMax(double colMax, double temp)
{
//Math.Abs(colMax);
if (temp < 0)
{
temp *= -1;
}
if (temp > colMax)
{
colMax = temp;
}
return colMax;
}
尝试以下内容:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string folder = @"D:\Desktop\..";
ReadTextFile readTextFile = new ReadTextFile(folder);
}
}
public class ReadTextFile
{
public List<string> list { get; set; }
public List<Double> values { get; set; }
public ReadTextFile(string folder)
{
DirectoryInfo info = new DirectoryInfo(folder);
FileInfo[] Files = info.GetFiles("*.txt");
list = new List<string>();
values = new List<Double>();
foreach (FileInfo file in Files)
{
string name = info.Name;
list.Add(file.Name);
string[] lines = System.IO.File.ReadAllLines(folder + file,
Encoding.GetEncoding("windows-1254"));
values.AddRange(MultiColumns(lines));
}
}
private List<double> MultiColumns(String[] strs)
{
double col1Max = 0;
double col2Max = 0;
double col3Max = 0;
var list = new List<Double>();
var format = new NumberFormatInfo();
format.NegativeSign = "-";
format.NumberNegativePattern = 1;
format.NumberDecimalSeparator = ".";
foreach (var row in strs)
{
var rowElements = row.Split(',');
Double temp1 = 0;
Double temp2 = 0;
Double temp3 = 0;
Double.TryParse(rowElements[0], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp1);
Double.TryParse(rowElements[1], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp2);
Double.TryParse(rowElements[2], NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, format, out temp3);
col1Max = getMax(col1Max, temp1);
col2Max = getMax(col2Max, temp2);
col3Max = getMax(col3Max, temp3);
}
list.Add(col1Max);
list.Add(col2Max);
list.Add(col3Max);
return list;
}
private double getMax(double colMax, double temp)
{
//Math.Abs(colMax);
if (temp < 0)
{
temp *= -1;
}
if (temp > colMax)
{
colMax = temp;
}
return colMax;
}
}
}
您可以简单地将Linq作为单行代码执行所有这些操作:
var results = Directory.GetFiles(@"D:\Desktop\..","*.txt")
.Select(file => new { file, max = File.ReadAllLines().Select(x=> double.Parse(x)).Max()});
如果您还希望获得所有最大值中的最大值:
allMax = result.Max(x => x.max);
测试:
foreach(var item in results)
Console.WriteLine($"file: {item.file} - max: {item.max}");