C# SaveFileDialog 、OpenFileDialog 的用法

18 篇文章 1 订阅

文件操作中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.  
  10. ofd.Multiselect = true;//设置多选

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

  12. {

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

  14. {

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

  16. }

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

  18. {

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

  20. }

  21. }

  22. }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值