こんにちは私はcsvファイルを作成するこのコードを検索しましたが、これはデフォルトの場所を示すことによってのみ動作します。ファイルの場所を定義したい場合はどうすればよいですか?CSVファイルをユーザーが選択できる場所に保存するにはどうすればよいですか?
private void btnExport_Click(object sender, EventArgs e)
{
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
//Add the Header row for CSV file.
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
csv += column.HeaderText + ',';
}
//Add new line.
csv += "\r\n";
//Adding the Rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
//Add the Data rows.
csv += cell.Value.ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Exporting to CSV.
string folderPath = "C:\\CSV\\";
File.WriteAllText(folderPath + "DataGridViewExport.csv", csv);
}
SaveFileDialogを使用 – PinBack
Sidenote:GUIスレッドでこれを行わないでください。アプリケーションが大量のデータセットに対して応答しなくなる可能性があります。 – Fildor
リファレンス:https://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog(v=vs.110).aspx –