C#(Winform)的SaveFileDialog(文件保存对话框)控件使用
#region 保存对话框
private void ShowSaveFileDialog()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "数据库备份文件(*.bak)|*.bak|数据文件(*.mdf)|*.mdf|日志文件(*.ldf)|*.ldf";
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//设置默认的文件名(本人测试没成功)
sfd.DefaultFileName = "YourFileName";// in wpf is sfd.FileName = "YourFileName";
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = sfd.FileName.ToString(); //获得文件路径
string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
string filePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));//获取文件路径,不带文件名
//给文件名前加上时间
//newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;
//在文件名里加字符
//saveFileDialog1.FileName.Insert(1,"dameng");
System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile();//输出文件
//fs输出带文字或图片的文件,就看需求了
}
}
C# 打开以对话框,获取文件夹路径 、文件的路径、文件名
// 获取文件夹绝对路径 显示在 txtbox 控件里
System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
this.txtboxPath.Text = folder.SelectedPath;
}
// 获取文件和路径名 一起显示在 txtbox 控件里
OpenFileDialog dialog = new OpenFileDialog();
if (dialog .ShowDialog() == DialogResult.OK)
{
this.txtboxPath.SelectedText = dialog.FileName;
}
//如果只显示文件名
OpenFileDialog dialog = new OpenFileDialog();
if (dialog .ShowDialog() == DialogResult.OK)
{
this.txtboxPath.SelectedText = dialog.SafeFileName;
}
C# winform 窗体弹出选择目录或文件 的对话框
//弹出一个选择目录的对话框
privatevoid btnSelectPath_Click(object sender, EventArgs e) //弹出一个选择目录的对话框
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.txtPath.Text = path.SelectedPath;
}
//弹出一个选择文件的对话框
privatevoid btnSelectFile_Click(object sender, EventArgs e) //弹出一个选择文件的对话框
{
OpenFileDialog file = new OpenFileDialog();
file.ShowDialog();
this.txtFile.Text = file.SafeFileName;
}
C# WinForm 得到保存文件对话框保存的文件路径、文件名
最新推荐文章于 2024-05-13 10:27:39 发布