我有一個Windows Forms應用程序能夠啓動調試控制檯。在應用程序中,通過點擊菜單,我讀取一個CSV文件並將其寫入控制檯。這個功能在下面。C#:Windows應用程序讀取CSV文件並寫入控制檯問題
protected void menuRead_Click(object sender, EventArgs e)
{
// ... functionality to load CSV files
System.IO.Stream inputDataFile = null;
OpenFileDialog fd = new OpenFileDialog();
fd.InitialDirectory = "c:\\";
fd.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
fd.FilterIndex = 1;
fd.RestoreDirectory = true;
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
if ((inputDataFile = fd.OpenFile()) != null)
{
inputData_exists = true;
// ... read input data from CSV file
using (CsvFileReader reader = new CsvFileReader(inputDataFile))
{
CsvRow row = new CsvRow();
while (reader.ReadRow(row))
{
foreach (string s in row)
{
Console.Write(s);
Console.Write(" ");
}
Console.WriteLine();
}
// ... close the input data stream
inputDataFile.Close();
}
}
}
catch (Exception err)
{
//Inform the user if can't read the file
MessageBox.Show(err.Message);
}
}
}
,一切工作正常,除了以下幾點: CSV文件有大約1200行代碼。執行此代碼時,OpenFileDialog()窗口只會在csv文件內容開始寫入控制檯窗口之前部分關閉。所以,我可以看到寫入到控制檯窗口的數據,並且我的窗體上顯示了一個對話窗口的小矩形部分。在數據寫入控制檯之前,有什麼方法可以確保關閉對話框?我應該打開一個新的線程與控制檯通信嗎?任何意見或幫助將不勝感激。謝謝。
這會在每行讀取後強制重繪。這看起來像是資源的浪費,但卻隱藏着信息。 – Zeos6
也許你可以嘗試fd.Invalidate(); –
@ Zeos6:不,它不會。你的閱讀循環從下面開始。 – ChrisWue