c#-将csv文件导出到用户桌面以进行部署

我已经建立了这个应用程式.它工作正常,但所有事情都遇到了麻烦,但一件麻烦事就是将导出的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";

我如何对其进行修改,使其可以成为部署该应用程序的任何计算机的默认导出位置?

解决方法:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), string.format("{0}.csv", comboBox5.Text));
上一篇:C#ConfigurationManager从app.config检索错误的连接字符串


下一篇:c#-使用带有固定位置文本的ScrollableControl的自定义控件