好的,我已经做到了这一点,但现在我有点陷入困境。
我创建了一个保存并加载学生成绩的窗体。问题是保存和加载方向是硬编码的,我想使用filedialog来保存和加载文件。
我并不确定如何做到这一点。有什么想法吗?
private void btnLoad_Click(object sender, EventArgs e)
{
string filePath = @"C:\Users\grades.txt";
if (File.Exists(filePath))
{
StreamReader stream = new StreamReader(filePath);
txtResult.AppendText(stream.ReadToEnd());
lblStatus.Text = "File Loaded";
}
else
MessageBox.Show("There was a problem loading the file");
}
private void btnSave_Click(object sender, EventArgs e)
{
lblStatus.Text = "Entry saved";//shows in status label
//string that specifies the location of the .txt file
string filePath = @"C:\Users\grades.txt";
StreamWriter fileWriter = new StreamWriter(filePath, true);//creates new object of class StreamWriter to be able to write to file
//enters the information from the different textboxes to the file
fileWriter.WriteLine(txtLastName.Text + ", " + txtFirstName.Text + ":\t" + Convert.ToString(txtID.Text) +
"\t" + txtClass.Text + "\t" + txtGrades.Text);
fileWriter.Close();//closes filewriter
}
}
编辑: 新代码有改进(我还没有实现Aybe的建议)。
我觉得我有点迟钝,但为什么这不起作用?在我看来,这应该有效,但它并没有。当我尝试加载文件时没有任何事情发生......
using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication14
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
dlg.InitialDirectory = "c:\\";
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() != DialogResult.OK)
return;
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Stream myStream;
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter fileWriter = new StreamWriter(myStream);//creates new object of class StreamWriter to be able to write to file
//enters the information from the different textboxes to the file
fileWriter.WriteLine(txtLastName.Text + ", " + txtFirstName.Text + ":\t" + Convert.ToString(txtID.Text) +
"\t" + txtClass.Text + "\t" + txtGrades.Text);
fileWriter.Close();//closes filewriter
myStream.Close();
}
}
}
}
}
答案 0 :(得分:-1)
首先,您将初始化对话框:
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 2 ;
saveFileDialog1.RestoreDirectory = true ;
然后,您将显示它并等待用户导航到其路径并键入文件名:
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
}
}
答案 1 :(得分:-1)
除@bwoogie回答外,利用using
关键字轻松处理资源:
using System;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonOpen_Click(object sender, EventArgs e)
{
using (var dialog = new OpenFileDialog())
{
if (dialog.ShowDialog() != DialogResult.OK)
return;
using (var reader = new StreamReader(dialog.OpenFile()))
{
// TODO read file
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
using (var dialog = new SaveFileDialog())
{
if (dialog.ShowDialog() != DialogResult.OK)
return;
using (var writer = new StreamWriter(dialog.OpenFile()))
{
// TODO save file
}
}
}
}
}