C# SaveFileDialog 、OpenFileDialog 的用法

发布时间:2025-12-09 16:25:28 浏览次数:10

文件操作中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

 
  • private void btnOpen_Click(object sender, EventArgs e)

  • {

  • OpenFileDialog ofd = new OpenFileDialog();

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

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

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

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

  • ofd.RestoreDirectory = true;

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

  • {

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

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

  • }

  • }

  • 2.OpenFileDialog选择多个文件

     
  • private void button3_Click(object sender, EventArgs e)

  • {

  • OpenFileDialog ofd = new OpenFileDialog();

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

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

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

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

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

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

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

  • {

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

  • {

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

  • }

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

  • {

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

  • }

  • }

  • }

  • 3.SaveFileDialog

     
  • private void button2_Click(object sender, EventArgs e)

  • {

  • SaveFileDialog sfd=new SaveFileDialog();

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

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

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

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

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

  • {

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

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

  • {

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

  • }

  • }

  • MessageBox.Show("ok");

  • }

  •  
  • private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)

  • {

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

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

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

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

  • if (扩展名 == "txt")

  • { }

  • if (扩展名 == "xml")

  • { }

  • }

  • 4.FolderBrowserDialog

     
  • string defaultPath = "";

  • FolderBrowserDialog dialog = new FolderBrowserDialog();

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

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

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

  • dialog.ShowNewFolderButton = false;

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

  • if (defaultPath != "")

  • {

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

  • dialog.SelectedPath = defaultPath;

  • }

  • //按下确定选择的按钮

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

  • {

  • //记录选中的目录

  • defaultPath = dialog.SelectedPath;

  • }

  • 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

    需要做网站?需要网络推广?欢迎咨询客户经理 13272073477