1 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
我做了一些假設(shè),因?yàn)槟鷽]有更新您的問題,但假設(shè)文件行包含格式為: 的行"Score: [score] ~[Name] -- Timer: [mm:ss] -- [numCorrect]/41",并且score是 adouble并且numCorrect是 an int(您沒有顯示它們來自哪里),那么這里是處理這種情況的一種方法。
首先,使用要存儲的屬性創(chuàng)建一個(gè)類,該類能夠從字符串(文件行)創(chuàng)建自身的實(shí)例并將自身輸出為字符串(用于寫入文件):
private class Result
{
public string Name { get; set; }
public double Score { get; set; }
public TimeSpan Time { get; set; }
public int CorrectCount { get; set; }
/// <summary>
/// Returns an instance of the Result class based on a string.
/// The string must be in the format:
/// "Score: [score] ~[Name] -- Timer: [mm:ss] -- [numCorrect]/41"
/// Where [score] is a valid double and [numCorrect] a valid int
/// </summary>
/// <param name="input">The string to parse</param>
/// <returns>A Result with properties set from the input string</returns>
public static Result Parse(string input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
var splitStrings = new[] {"Score:", " ~", " -- ", "/41"};
var parts = input
.Split(splitStrings, StringSplitOptions.RemoveEmptyEntries)
.Select(item => item.Trim())
.ToList();
// These will hold the converted parts of the string
double score;
int correctCount;
TimeSpan time;
// Verify that the string contains 4 parts, and that the Score, Time, and
// CorrectCount parts can be converted to the proper data type for the property
if (parts.Count != 4 ||
!double.TryParse(parts[0], out score) ||
!TimeSpan.TryParseExact(parts[2], @"mm\:ss",
CultureInfo.InvariantCulture, out time) ||
!int.TryParse(parts[3], out correctCount))
{
throw new FormatException("input is not in a recognized format");
}
return new Result
{
Name = parts[1],
Score = score,
Time = time,
CorrectCount = correctCount
};
}
public override string ToString()
{
return $"Score:{Score} ~{Name} -- {Time.ToString(@"mm\:ss")} -- {CorrectCount}/41";
}
}
然后創(chuàng)建一個(gè)可以從表單數(shù)據(jù)創(chuàng)建此類實(shí)例的方法:
// Not sure where these come from so created these class fields
private const string Path = @"f:\public\temp\score.txt";
private double score = 0;
private int numCorrect = 0;
private static Result GetResultFromFormData()
{
return new Result
{
Score = score,
Name = textBox1.Text,
Time = TimeSpan.ParseExact(label9.Text, @"mm\:ss", CultureInfo.InvariantCulture),
CorrectCount = numCorrect)
};
}
現(xiàn)在我們可以從文件內(nèi)容和表單中填充這些類的列表。然后我們可以使用Linq我們想要的任何字段對列表進(jìn)行排序(Score在這種情況下),并將排序后的列表寫回文件:
private void button1_Click(object sender, EventArgs e)//Submit Button
{
if (isDone)
{
// Create a list of results from our file
List<Result> existingResults = File.ReadAllLines(Path).Select(Result.Parse).ToList();
// Add a new result to the list from the form data
existingResults.Add(GetResultFromFormData());
// Sort the list on the Score property
existingResults = existingResults.OrderBy(result => result.Score).ToList();
// Write the sorted list back to the file
File.WriteAllLines(Path, existingResults.Select(result => result.ToString()));
}
}
現(xiàn)在文件包含它的原始內(nèi)容,加上表單的新結(jié)果,全部按Score.
- 1 回答
- 0 關(guān)注
- 94 瀏覽
添加回答
舉報(bào)