[英]C# visual studio user input for file path and file name
I have the current code below to save a .csv file on the button click event. 我有下面的当前代码,以将.csv文件保存在按钮click事件上。 However, I want the user to be able to choose the file path and document name.
但是,我希望用户能够选择文件路径和文档名称。 Any suggestions on how to go about that?
有什么建议吗? I could read the text from a text box for a file name, but what about the file path with out the user having to type of it in?
我可以从文本框中读取文件名的文本,但是如果用户不必键入文件名,文件路径又如何呢?
long[][] finalResultArray = dataList.Select(a => a.ToArray()).ToArray();
string filePath = @"C:\test\test.csv";
int length = finalResultArray.GetLength(0);
StringBuilder sb = new StringBuilder();
for(int index = 0; index < length; index++)
{
sb.AppendLine(string.Join(",", finalResultArray[index]));
}
File.WriteAllText(filePath, sb.ToString());
MessageBox.Show("Save Complete!");
You could use the SaveFileDialog
class for this: 您可以为此使用
SaveFileDialog
类:
SaveFileDialog saveFileDialog = new SaveFileDialog();
if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog.FileName, sb.ToString());
MessageBox.Show("Save Complete!");
}
If you want you can also try OpenFileDialog. 如果需要,也可以尝试使用OpenFileDialog。 System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
System.Windows.Forms.OpenFileDialog dlg =新System.Windows.Forms.OpenFileDialog(); dlg.ShowDialog();
dlg.ShowDialog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.