C# 中SaveFileDialog和OpenFileDialog的使用
我要制作一个文件软件备份数据库的界面在改界面中有如下控件TEXTBOX1BUTTON1要求点击BUTTON1弹出路径选择窗口然后把路径信息在TEXTBOX1.text中有...
我要制作一个文件软件备份数据库的界面
在改界面中有如下控件
TEXTBOX1 BUTTON1
要求点击BUTTON1 弹出 路径选择窗口
然后把路径信息在TEXTBOX1.text中
有人说用SaveFileDialog和OpenFileDialog组件可以
向大家请教一下 展开
在改界面中有如下控件
TEXTBOX1 BUTTON1
要求点击BUTTON1 弹出 路径选择窗口
然后把路径信息在TEXTBOX1.text中
有人说用SaveFileDialog和OpenFileDialog组件可以
向大家请教一下 展开
5个回答
展开全部
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();
}
}
你试一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
这个负责选择保存路径
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if (openfiledialog1.showdialog() == dialogresult.ok)
{
var path = openfiledialog1.filename;
}
if (savefiledialog1.showdialog() == dialogresult.ok)
{
var path = savefiledialog1.filename;
}
{
var path = openfiledialog1.filename;
}
if (savefiledialog1.showdialog() == dialogresult.ok)
{
var path = savefiledialog1.filename;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼的可以,不过拖了组件就不用new了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
y晕了
拖一个SaveFileDialog 到你窗体 你不就明白了吗 ?
拖一个SaveFileDialog 到你窗体 你不就明白了吗 ?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询