我有我建的这个应用程序。它工作正常,除了一件事我遇到的麻烦就是将我导出的csv文件保存到用户的桌面。详细说明:我通常会硬编码要导出文件的路径,但是在部署的情况下,这是不可行的,因为这意味着每次为用户的机器改变路径。如何为所有用户默认将导出的路径保存到桌面?以下是我的代码
private void button6_Click_2(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBox5.Text))
{
MessageBox.Show("Cannot export unless table name is specified!");
}
else
{
int count_row = dataGridView1.RowCount;
int count_cell = dataGridView1.Rows[0].Cells.Count;
string path = "C:\\Users\\Jevon\\Desktop\\" + comboBox5.Text + ".csv";
string rxHeader = "Code" + "," + "Description" + "," + "NDC" + "," + "Supplier Code"
+ "," + "Supplier Description" + "," + "Pack Size" + "," + "UOM" + Environment.NewLine;
MessageBox.Show("Please wait while " + comboBox5.Text + " table is being exported..");
for (int row_index = 0; row_index <= count_row - 2; row_index++)
{
for (int cell_index = 1; cell_index <= count_cell - 1; cell_index++)
{
textBox8.Text = textBox8.Text + dataGridView1.Rows[row_index].Cells[cell_index].Value.ToString() + ",";
}
textBox8.Text = textBox8.Text + "\r\n";
if (!File.Exists(path))
{
System.IO.File.WriteAllText(path, rxHeader);
System.IO.File.AppendAllText(path, textBox8.Text);
}
else
{
System.IO.File.AppendAllText(path, textBox8.Text);
textBox8.Clear();
}
}
MessageBox.Show("Export of " + comboBox5.Text + " table is complete!");
}
}
正如您所看到的,这是路径:
string path = "C:\\Users\\Jevon\\Desktop\\" + comboBox5.Text + ".csv";
如何修改它以使其成为部署应用程序的任何计算机的默认导出位置?
答案 0 :(得分:3)
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), string.format("{0}.csv", comboBox5.Text));