文件操作中SaveFileDialog的用法

c#获取要保存文件的对话框,用SaveFileDialog类。具体用法很简单分享一下吧,对于初学者可能有用

//可能要获取的路径名
string localFilePath = "", fileNameExt= "", newFileName= "", FilePath = "";
SaveFileDialog saveFileDialog = new SaveFileDialog();

//设置文件类型
//书写规则例如:txt files(*.txt)|*.txt
saveFileDialog.Filter = "txt files(*.txt)|*.txt|xls files(*.xls)|*.xls|All files(*.*)|*.*";
//设置默认文件名(可以不设置)
saveFileDialog.FileName = "siling-Data";
//主设置默认文件extension(可以不设置)
saveFileDialog.DefaultExt = "xml";
//获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
saveFileDialog.AddExtension = true;

//设置默认文件类型显示顺序(可以不设置)
saveFileDialog.FilterIndex = 2;

//保存对话框是否记忆上次打开的目录
saveFileDialog.RestoreDirectory = true;

// Show save file dialog box
DialogResult result = saveFileDialog.ShowDialog();
//点了保存按钮进入
if (result == DialogResult.OK)
{
  //获得文件路径
  localFilePath = saveFileDialog.FileName.ToString();

//获取文件名,不带路径
  //fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);

//获取文件路径,不带文件名
  //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));

//给文件名前加上时间
  //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;

//在文件名里加字符
  //saveFileDialog.FileName.Insert(1,"dameng");
  //为用户使用 SaveFileDialog 选定的文件名创建读/写文件流。
  //System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog.OpenFile();//输出文件

//fs可以用于其他要写入的操作
}

————————————————
版权声明:本文为CSDN博主「Denghejing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Denghejing/article/details/46423065

saveFileDialog saveFileDialog1=new saveFileDialog();

saveFileDialog1.InitialDirectory = Path.GetDirectoryName(strPartPath);

//设置文件类型

saveFileDialog1.Filter = "Excel 工作簿(*.xlsx)|*.xlsx|Excel 启动宏的工作簿(*.xlsm)|*.xlsm|Excel 97-2003工作簿(*.xls)|*.xls";

//saveFileDialog1.FilterIndex = 1;//设置文件类型显示

saveFileDialog1.FileName = "自己取个";//设置默认文件名

saveFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录

saveFileDialog1.CheckPathExists = true;//检查目录

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

string strSaveFileLocation = saveFileDialog1.FileName;//文件路径

}

其它:
SaveFileDialog saveFileDialog = new SaveFileDialog();
//打开的文件选择对话框上的标题
saveFileDialog.Title = "请选择文件";
//设置文件类型
saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
//设置默认文件类型显示顺序
saveFileDialog.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
saveFileDialog.RestoreDirectory = true;
//设置是否允许多选
saveFileDialog.Multiselect = false;
//按下确定选择的按钮
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
    //获得文件路径
    string localFilePath = saveFileDialog.FileName.ToString();
    //获取文件路径,不带文件名
    //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
    //获取文件名,带后缀名,不带路径
    string fileNameWithSuffix = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
    //去除文件后缀名
    string fileNameWithoutSuffix = fileNameWithSuffix.Substring(0, fileNameWithSuffix.LastIndexOf("."));
    //在文件名前加上时间
    string fileNameWithTime = DateTime.Now.ToString("yyyy-MM-dd ") + fileNameExt;
    //在文件名里加字符
    string newFileName = localFilePath.Insert(1, "Tets");
}

https://blog.csdn.net/u011108093/article/details/81627935

1.OpenFileDialog

 
  1. private void btnOpen_Click(object sender, EventArgs e)

  2. {

  3. OpenFileDialog ofd = new OpenFileDialog();

  4. ofd.InitialDirectory = @"C:\Users\LWP1398\Desktop"; //设置初始路径

  5. ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容

  6. ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv

  7. ofd.Title = "打开文件"; //获取或设置文件对话框标题

  8. ofd.RestoreDirectory = true;

  9. if (ofd.ShowDialog() == DialogResult.OK)

  10. {

  11. //FileName:所选文件的全路径 SafeFileName:所选的文件名

  12. txtPath.Text = "FileName:" + ofd.FileName + "\r\n" + "SafeFileName:" + ofd.SafeFileName;

  13. }

  14. }

2.OpenFileDialog选择多个文件

 
  1. private void button3_Click(object sender, EventArgs e)

  2. {

  3. OpenFileDialog ofd = new OpenFileDialog();

  4. ofd.InitialDirectory = @"C:\Users\LWP1398\Desktop"; //设置初始路径

  5. ofd.Filter = "Excel文件(*.xls)|*.xls|Csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //设置“另存为文件类型”或“文件类型”框中出现的选择内容

  6. ofd.FilterIndex = 2; //设置默认显示文件类型为Csv文件(*.csv)|*.csv

  7. ofd.Title = "打开文件"; //获取或设置文件对话框标题

  8. ofd.RestoreDirectory = true;设置对话框是否记忆上次打开的目录

  9. ofd.Multiselect = true;//设置多选

  10. if (ofd.ShowDialog() == DialogResult.OK)

  11. {

  12. for (int i = 0; i < ofd.FileNames.Length; i++)

  13. {

  14. txtPath.Text += ofd.FileNames[i] + "\r\n";//输出一个路径回车换行

  15. }

  16. for (int i = 0; i < ofd.FileNames.Length; i++)

  17. {

  18. txtPath.Text += ofd.SafeFileNames[i] + "\r\n";

  19. }

  20. }

  21. }

3.SaveFileDialog

 
  1. private void button2_Click(object sender, EventArgs e)

  2. {

  3. SaveFileDialog sfd=new SaveFileDialog();

  4. sfd.Filter = "文本文件(*.txt)|*.txt|所有文件|*.*";//设置文件类型

  5. sfd.FileName = "保存";//设置默认文件名

  6. sfd.DefaultExt = "txt";//设置默认格式(可以不设)

  7. sfd.AddExtension = true;//设置自动在文件名中添加扩展名

  8. if (sfd.ShowDialog()==DialogResult.OK)

  9. {

  10. txtPath.Text = "FileName:" + sfd.FileName + "\r\n" ;

  11. using (StreamWriter sw = new StreamWriter(sfd.FileName))

  12. {

  13. sw.WriteLineAsync("今天是个好天气");

  14. }

  15. }

  16. MessageBox.Show("ok");

  17. }

 
  1. private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)

  2. {

  3. saveFileDialog1.AddExtension = true; //自动添加扩展名

  4. e.Cancel = true; //取消保存操作

  5. string 扩展名 = System.IO.Path.GetExtension(saveFileDialog1.FileName);

  6. //判断扩展名并实现自定义的保存操作(导出)

  7. if (扩展名 == "txt")

  8. { }

  9. if (扩展名 == "xml")

  10. { }

  11. }

4.FolderBrowserDialog

 
  1. string defaultPath = "";

  2. FolderBrowserDialog dialog = new FolderBrowserDialog();

  3. //打开的文件夹浏览对话框上的描述

  4. dialog.Description = "请选择一个文件夹";

  5. //是否显示对话框左下角 新建文件夹 按钮,默认为 true

  6. dialog.ShowNewFolderButton = false;

  7. //首次defaultPath为空,按FolderBrowserDialog默认设置(即桌面)选择

  8. if (defaultPath != "")

  9. {

  10. //设置此次默认目录为上一次选中目录

  11. dialog.SelectedPath = defaultPath;

  12. }

  13. //按下确定选择的按钮

  14. if (dialog.ShowDialog() == DialogResult.OK)

  15. {

  16. //记录选中的目录

  17. defaultPath = dialog.SelectedPath;

  18. }

  19. MessageBox.show(defaultPath);

C#中利用OpenFileDialog与 SaveFileDialog保存文件与创建文件 以及FolderBrowserDialog用法

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;   
            }
        }

————————————————
版权声明:本文为CSDN博主「已被格式化的叔叔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sl1990129/article/details/79037518

C# SaveFileDialog 、OpenFileDialog 的用法相关推荐

  1. C#的OpenFileDialog和SaveFileDialog的常见用法

    OpenFileDialog openFileDialog1 = new OpenFileDialog();             openFileDialog1.InitialDirectory ...

  2. C#中SaveFileDialog 和OpenFileDialog 的用法

    1.OpenFileDialog private void btnOpen_Click(object sender, EventArgs e){OpenFileDialog ofd = new Ope ...

  3. C#winform中OpenFileDialog的用法

    private void btnOpenFileDialog_Click(object sender, EventArgs e){OpenFileDialog oFD = new OpenFileDi ...

  4. 【C#笔记】SaveFileDialog的简单用法

    SaveFileDialog savefiledialog = new SaveFileDialog(); //设置文件类型 savefiledialog.Filter = "文本文件(*. ...

  5. C#中打开文件和自动生成缩略图之我建

    在这里我介绍一种简单的在Winforms中生成缩略图的代码. 如下: private void PostPic(string fileName)  //fileName为需要处理的图片的绝对路径  { ...

  6. C#中SaveFileDialog和OpenFileDiaLog 用法

    C#中SaveFileDialog和OpenFileDiaLog 用法 介绍SaveFileDialog以及OpenFileDiaLog的使用,把textbox中的内容通过SaveFileDialog ...

  7. SaveFileDialog 和OpenFileDialog

    1. OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.tx ...

  8. 在线程中调用OpenFileDialog/SaveFileDialog报错处理方法

    当在一个线程中使用 OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); 会报如下错误: 在可以调用 OLE 之前,必须将当前线程设 ...

  9. SaveFileDialog的用法

    c#获取要保存文件的对话框,用SaveFileDialog类.具体用法很简单分享一下吧,对于初学者可能有用 //可能要获取的路径名 string localFilePath = "" ...

最新文章

  1. eclipse打包项目为aar_eclipse中将项目打包成jar的两种方法,及其问题与解决方法...
  2. 指针06 - 零基础入门学习C语言46
  3. JPA实体关联关系,一对一以及转换器
  4. vue 实现数据滚动显示_vue 滚动加载数据
  5. 2019我做成的事情
  6. 计算机基础知识复习资料,计算机基础知识复习资料
  7. 一个数组分成两部分,让两部分的差最小
  8. PostgreSQL 数据库NULL值的默认排序行为与查询、索引定义规范 - nulls first\last, asc\desc...
  9. 越混越差的十个原因,看看你有没有?
  10. 奥鹏2019计算机应用基础1,奥鹏东师2019年秋季(计算机应用基础)离线考核(仅供参考).docx...
  11. 几个学习git的地方
  12. win10 网卡驱动正常搜索不到WiFi信号 解决方法
  13. 非确定有限自动机转换为确定有限自动机
  14. iOS:error: unable to read input file
  15. cs1.6一直连接服务器,CS1.6连接不上服务器解决办法
  16. win10 安装硕正
  17. 全网最全AD16——原理图绘制
  18. 国产旗舰手机价格泡沫严重,二手比新机更划算,要不然就买iPhone
  19. 爱因互动王守崑:未来机器人的服务质量会显著高于人类
  20. 网络准入控制产品发展

热门文章

  1. [RK3568 Android11] 教程之watchdog看门狗应用
  2. wait, WIFEXITED, WEXITSTATUS
  3. Spark考试题总结
  4. finally在python中是什么意思_“finally”总是在Python中执行吗?
  5. 力创eda 画布大小_立创EDA快速上手经验篇 -
  6. 最近地震也太太太频繁了吧?! | Alfred数据室
  7. 电子商务与营销管理创新
  8. Flex 3编程入门级教程附代码下载
  9. 计算机主板芯片知识点,【写给小白的硬件知识点】什么是主板,主板的作用
  10. android应用程序开发(第二版)课后习题答案.doc,android移动应用开发习题答案.pdf