1.利用 SaveFileDialog保存并创建文件:根据用户指定的路径选择并保存文件,单个文件
using System.IO;
private string saveFile()
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "文本(*.txt)|*.txt;|Excle(*.xls)|*.xls";
if (saveDlg.ShowDialog() == DialogResult.OK)
{
FileStream fs1 = new FileStream(saveDlg.FileName, FileMode.Create, FileAccess.Write);
fs1.Close();
}
return saveDlg.FileName;
}
2.利用
OpenFileDialog打开用户指定的路径并删除文件:用来读取单个文件
private string clearFile()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "文本(*.txt)|*.txt;|Excle(*.xls)|*.xls";
openFileDialog1.Title = "请选择要清空的文件";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
CreateFilePath = openFileDialog1.FileName;
}
if (File.Exists(CreateFilePath))
{
using (StreamWriter sw = new StreamWriter(CreateFilePath, false))//fasle ,若存在则覆盖
{
sw.WriteLine("");
sw.Close();
}
}
}
3.
FolderBrowserDialog:用来选择一个文件夹,从而读取这个文件夹下面的所有文件
private void btnBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtFile.Text = folderBrowserDialog1.SelectedPath;
}
}