gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:58:07 25 4
gpt4 key购买 nike

我已经构建了这个应用程序。它工作正常,除了将我导出的 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# - 将 csv 文件导出到用户桌面以进行部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43906425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com