我做了一些研究,但沒有運氣,還是相對很新的總體規劃,對代碼效率任何提示都大加讚賞。如何將我的收藏內容寫入CSV文件?
那麼我將如何去運行它通過我的分級功能中的一個或更好,但一個CSV文件,該文件是逗號分隔後寫我的收藏+等級的內容出一個文本文件。
我不斷收到類似的問題cannot convert from 'string' to string[]
。我知道我可能在這裏錯過了一步,但我很難弄清楚它是什麼。
這是我的列表格式,我從一個CSV文件中加載的值,然後分別存入FirstLastName和MarksValue。
下面是我的分級單選按鈕,我設法讓他們打印我的函數+ FirstLastName和MarksValue的輸出到消息框,所以這是我的一個開始。
public class Marks
{
public string FirstLastName { get; set; }
public int MarksValue { get; set; }
//public string mySecondaryGrade { get; set; }
}
private void buttongradeStart_Click(object sender, RoutedEventArgs e)
{
// This runs through an if statement based on my radio buttons.
//Testing that radio buttons work with my if else if statements
// will change values to my grade function later
Grade grader = new Grade();
VET vetGrader = new VET();
PolyOveride overideVet = new PolyOveride();
Helper myhelperArray = new Helper();
string v;
//This is a check to see if any data has been loaded.
if (dLoad == true)
{
//var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
System.Windows.Forms.SaveFileDialog saveDialogue = new System.Windows.Forms.SaveFileDialog();
saveDialogue.Title = "Save File";
saveDialogue.FileName = "Graded-Assessment1.csv";
saveDialogue.Filter = "CSV Files(*.csv)|*.csv|Text Files(*.txt)|*.txt|All files(*.*)|*.*";
saveDialogue.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (saveDialogue.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var path = saveDialogue.FileName;
if (radioCollegeGrade.IsChecked == true)
{
foreach (Marks mMarks in myhelperArray)
{
File.WriteAllLines(path, v);
v = grader.CollegeGrade(mMarks.MarksValue);
System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v);
//System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.csv");
//file.WriteLine(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v);
//file.Close();
}
}
else if (radioVetGrade.IsChecked == true)
{
foreach (Marks mMarks in myhelperArray)
{
v = vetGrader.VETGrade(mMarks.MarksValue);
System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v);
}
}
else if (radioCompetency.IsChecked == true)
{
foreach (Marks mMarks in myhelperArray)
{
v = overideVet.VETGrade(mMarks.MarksValue);
System.Windows.Forms.MessageBox.Show(mMarks.FirstLastName + " " + mMarks.MarksValue + " " + v);
}
}
else
{
System.Windows.Forms.MessageBox.Show("I am not sure how you deslected a box!, you should probably select an option.");
}
}
else
{
System.Windows.Forms.MessageBox.Show("Placeholder Message");
}
}
else
{
System.Windows.Forms.MessageBox.Show("You have not selected a file for grading");
}
}
不知道有關這是怎樣,但是這是我的助手類
public class Helper
{
public static List<Marks> arrayCSV = new List<Marks>();
public IEnumerator<Marks> GetEnumerator() { return arrayCSV.GetEnumerator(); }
}
我建議使用CsvHelper NuGet包 - 見https://joshclose.github.io/CsvHelper/的例子。編寫自己的csv輸出看起來很簡單,但有很多問題。例如,如果用戶名有逗號,會發生什麼情況?站在巨人的肩膀上! – tofutim
好文檔那裏交配,謝謝。 –
_站在巨人的肩膀上!_ ..並希望巨人不會倒下;) – Fabio