我希望避免为每次保存尝试弹出对话框并选择一次文件名,然后在不询问文件名后保存?
理想情况下,应用应该显示一次对话框,保存文件名。然后将代码保存数据封装到方法中,使文件名可见,并在需要时调用它。
不知道该怎么做。
private void SaveData() //Figure out how to remeber previous save name
{
string taxpayerLine;
string taxpayerFile;
SaveFileDialog taxpayerFileChooser;
StreamWriter fileWriter;
taxpayerFileChooser = new SaveFileDialog();
taxpayerFileChooser.Filter = "All text files|*.txt";
if (taxpayerFileChooser.ShowDialog() == DialogResult.OK)
{
taxpayerFile = taxpayerFileChooser.FileName;
taxpayerFileChooser.Dispose();
fileWriter = new StreamWriter(taxpayerFile, true);
foreach (Taxpayer txp in taxpayerBindingSource)
{
taxpayerLine = txp.Display(",");
fileWriter.WriteLine(taxpayerLine);
}
fileWriter.Close();
fileWriter.Dispose(); //Destructor
MessageBox.Show("Data Saved to: "
+ System.IO.Path.GetFileNameWithoutExtension(taxpayerFile));