C# 中SaveFileDialog和OpenFileDialog的使用
答案:4 悬赏:60 手机版
解决时间 2021-03-25 05:14
- 提问者网友:轻浮
- 2021-03-24 13:37
C# 中SaveFileDialog和OpenFileDialog的使用
最佳答案
- 五星知识达人网友:刀戟声无边
- 2021-03-24 14:57
SaveFileDialog loSaveFile = new SaveFileDialog();
loSaveFile.Filter = "PDF文件(*.pdf)|*.pdf";
loSaveFile.Title = "保存PDF文档";
if (loSaveFile.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = loSaveFile.FileName;
}
//SaveFile();
这个负责选择保存路径
loSaveFile.Filter = "PDF文件(*.pdf)|*.pdf";
loSaveFile.Title = "保存PDF文档";
if (loSaveFile.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = loSaveFile.FileName;
}
//SaveFile();
这个负责选择保存路径
全部回答
- 1楼网友:躲不过心动
- 2021-03-24 17:17
y晕了
拖一个SaveFileDialog 到你窗体 你不就明白了吗 ?
拖一个SaveFileDialog 到你窗体 你不就明白了吗 ?
- 2楼网友:慢性怪人
- 2021-03-24 15:41
1楼的可以,不过拖了组件就不用new了。
- 3楼网友:过活
- 2021-03-24 15:13
StreamWriter writer;
StreamReader reader;
FileStream fs;
private void openBtn_Click(object sender, System.EventArgs e)
{
string theFile;
openFileDialog1.InitialDirectory=Application.ExecutablePath;
openFileDialog1.Filter="word Files(*.doc)|*.doc|All Files(*.*)|*.*";
if (openFileDialog1.ShowDialog()==DialogResult.OK )
{
theFile=openFileDialog1.FileName;
try
{
fs=new FileStream(theFile,FileMode.Open);
reader=new StreamReader(fs);
textBox1.Text=reader.ReadToEnd();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message);
}
finally
{
reader.Close();
fs.Close();
}
}
}
private void saveBtn_Click(object sender, System.EventArgs e)
{
string theFile;
saveFileDialog1.InitialDirectory=Application.ExecutablePath;
saveFileDialog1.Filter="word Files(*.doc)|*.doc|All Files(*.*)|*.*";
saveFileDialog1.OverwritePrompt=true;
saveFileDialog1.ShowDialog();
theFile=saveFileDialog1.FileName;
try
{
fs=new FileStream(theFile,FileMode.Create);
writer=new StreamWriter(fs);
writer.Write(textBox1.Text);
}
catch (Exception excep)
{
MessageBox.Show(excep.Message);
}
finally
{
writer.Flush();
writer.Close();
fs.Close();
}
}
你试一下
StreamReader reader;
FileStream fs;
private void openBtn_Click(object sender, System.EventArgs e)
{
string theFile;
openFileDialog1.InitialDirectory=Application.ExecutablePath;
openFileDialog1.Filter="word Files(*.doc)|*.doc|All Files(*.*)|*.*";
if (openFileDialog1.ShowDialog()==DialogResult.OK )
{
theFile=openFileDialog1.FileName;
try
{
fs=new FileStream(theFile,FileMode.Open);
reader=new StreamReader(fs);
textBox1.Text=reader.ReadToEnd();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message);
}
finally
{
reader.Close();
fs.Close();
}
}
}
private void saveBtn_Click(object sender, System.EventArgs e)
{
string theFile;
saveFileDialog1.InitialDirectory=Application.ExecutablePath;
saveFileDialog1.Filter="word Files(*.doc)|*.doc|All Files(*.*)|*.*";
saveFileDialog1.OverwritePrompt=true;
saveFileDialog1.ShowDialog();
theFile=saveFileDialog1.FileName;
try
{
fs=new FileStream(theFile,FileMode.Create);
writer=new StreamWriter(fs);
writer.Write(textBox1.Text);
}
catch (Exception excep)
{
MessageBox.Show(excep.Message);
}
finally
{
writer.Flush();
writer.Close();
fs.Close();
}
}
你试一下
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯