解决方案

C# SaveFileDialog 、OpenFileDialog 的用法

seo靠我 2023-09-25 08:32:02

文件操作中SaveFileDialog的用法

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

//可能要获取的路径名

string localFiSEO靠我lePath = "", fileNameExt= "", newFileName= "", FilePath = "";

SaveFileDialog saveFileDialog = new SavSEO靠我eFileDialog();

//设置文件类型

//书写规则例如:txt files(*.txt)|*.txt

saveFileDialog.Filter = "txt files(*.txt)|*.txtSEO靠我|xls files(*.xls)|*.xls|All files(*.*)|*.*";

//设置默认文件名(可以不设置)

saveFileDialog.FileName = "siling-Data";SEO靠我

//主设置默认文件extension(可以不设置)

saveFileDialog.DefaultExt = "xml";

//获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加SEO靠我扩展名。(可以不设置)

saveFileDialog.AddExtension = true;

//设置默认文件类型显示顺序(可以不设置)

saveFileDialog.FilterIndex = 2;

//SEO靠我保存对话框是否记忆上次打开的目录

saveFileDialog.RestoreDirectory = true;

// Show save file dialog box

DialogResult resuSEO靠我lt = saveFileDialog.ShowDialog();

//点了保存按钮进入

if (result == DialogResult.OK)

{

  //获得文件路径

localFilePath = saSEO靠我veFileDialog.FileName.ToString();

  //获取文件名,不带路径

//fileNameExt = localFilePath.Substring(localFilePath.LSEO靠我astIndexOf("\\") + 1);

  //获取文件路径,不带文件名

//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOSEO靠我f("\\"));

  //给文件名前加上时间

  //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt;

  //在文件名里加字符

//saveSEO靠我FileDialog.FileName.Insert(1,"dameng");

  //为用户使用 SaveFileDialog 选定的文件名创建读/写文件流。

//System.IO.FileStream SEO靠我fs = (System.IO.FileStream)saveFileDialog.OpenFile();//输出文件

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

}

————————————————

版权声明:本文为SEO靠我CSDN博主「Denghejing」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/Denghejing/aSEO靠我rticle/details/46423065

saveFileDialog saveFileDialog1=new saveFileDialog();

saveFileDialog1.InitialDiSEO靠我rectory = Path.GetDirectoryName(strPartPath);

  //设置文件类型

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

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

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

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

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

if (saveFileDialog1.ShowDialog() == DialogResuSEO靠我lt.OK)

{

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

 }       

其它:

SaveFileDialog saveFileDialogSEO靠我 = new SaveFileDialog();

//打开的文件选择对话框上的标题

saveFileDialog.Title = "请选择文件";

//设置文件类型

saveFileDialog.FilterSEO靠我 = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";

//设置默认文件类型显示顺序

saveFileDialog.FilterIndex = 1;

//保存对话框是否记忆上次打开的目录SEO靠我

saveFileDialog.RestoreDirectory = true;

//设置是否允许多选

saveFileDialog.Multiselect = false;

//按下确定选择的按钮

if (sSEO靠我aveFileDialog.ShowDialog() == DialogResult.OK)

{

    //获得文件路径

string localFilePath = saveFileDialog.FileNamSEO靠我e.ToString();

    //获取文件路径,不带文件名

//FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));SEO靠我

    //获取文件名,带后缀名,不带路径

string fileNameWithSuffix = localFilePath.Substring(localFilePath.LastIndexOf("\\")SEO靠我 + 1);

    //去除文件后缀名

string fileNameWithoutSuffix = fileNameWithSuffix.Substring(0, fileNameWithSuffix.LasSEO靠我tIndexOf("."));

    //在文件名前加上时间

string fileNameWithTime = DateTime.Now.ToString("yyyy-MM-dd ") + fileNameESEO靠我xt;

    //在文件名里加字符

    string newFileName = localFilePath.Insert(1, "Tets");

}

 

https://blog.csdn.net/u011108093/SEO靠我article/details/81627935

1.OpenFileDialog

 

private void btnOpen_Click(object sender, EventArgs e)

{

OpenFSEO靠我ileDialog ofd = new OpenFileDialog();

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

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

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

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

ofd.RestoreDirectoSEO靠我ry = true;

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

{

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

txtPath.TeSEO靠我xt = "FileName:" + ofd.FileName + "\r\n" + "SafeFileName:" + ofd.SafeFileName;

}

}

2.OpenFileDialog选择多个SEO靠我文件

 

private void button3_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

oSEO靠我fd.InitialDirectory = @"C:\Users\LWP1398\Desktop"; //设置初始路径

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

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

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

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

ofd.MultiselSEO靠我ect = true;//设置多选

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

{

for (int i = 0; i < ofd.FileNames.Length; SEO靠我i++)

{

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

}

for (int i = 0; i < ofd.FileNames.LengthSEO靠我; i++)

{

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

}

}

}

3.SaveFileDialog

 

private void button2_Click(oSEO靠我bject sender, EventArgs e)

{

SaveFileDialog sfd=new SaveFileDialog();

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

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

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

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

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

{

txtPath.Text = "FileName:" + sfd.FileNSEO靠我ame + "\r\n" ;

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

{

sw.WriteLineAsync("今天是个好天气");SEO靠我

}

}

MessageBox.Show("ok");

}

 

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)

{

saveFSEO靠我ileDialog1.AddExtension = true; //自动添加扩展名

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

string 扩展名 = System.IO.Path.GetExtSEO靠我ension(saveFileDialog1.FileName);

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

if (扩展名 == "txt")

{ }

if (扩展名 == "xml")

{ }

}

4.FoSEO靠我lderBrowserDialog

 

string defaultPath = "";

FolderBrowserDialog dialog = new FolderBrowserDialog();

//打开SEO靠我的文件夹浏览对话框上的描述

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

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

dialog.ShowNewFolderButtoSEO靠我n = false;

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

if (defaultPath != "")

{

//设置此次默认目录为上一次选中目录SEO靠我

dialog.SelectedPath = defaultPath;

}

//按下确定选择的按钮

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

{

//记录选中的目录

dSEO靠我efaultPath = dialog.SelectedPath;

}

MessageBox.show(defaultPath);

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

1.利用 SaveFileDialog保存并创建文件:根据用户指定的路径选择并保存文件,单个文件

     using System.IO;

privaSEO靠我te string saveFile()

        {

            SaveFileDialog saveDlg = new SaveFileDialog();

saveDlg.Filter = "文本(*.txt)|*.txtSEO靠我;|Excle(*.xls)|*.xls";

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

            {

FileStream fs1 = new FileStream(sSEO靠我aveDlg.FileName, FileMode.Create, FileAccess.Write);

                fs1.Close();

            }

            return saveDlg.FileName;

        }

2.利用 OpenFSEO靠我ileDialog打开用户指定的路径并删除文件:用来读取单个文件

private string clearFile()

        {

OpenFileDialog openFileDialog1 = new OpenSEO靠我FileDialog();

            openFileDialog1.Filter = "文本(*.txt)|*.txt;|Excle(*.xls)|*.xls";

openFileDialog1.Title = SEO靠我"请选择要清空的文件";

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

            {

CreateFilePath = openFileDialog1.FiSEO靠我leName;

            }

            if (File.Exists(CreateFilePath))

            {

using (StreamWriter sw = new StreamWriter(CreateFilePath, fSEO靠我alse))//fasle ,若存在则覆盖

                {

                    sw.WriteLine("");

                    sw.Close();

                }

            }

        }

3.    FolderBrowserDialog:用来选择一个文件夹,从而读取这个文件夹下面SEO靠我的所有文件

  private void btnBrowse_Click(object sender, EventArgs e)

        {

if (folderBrowserDialog1.ShowDialog() SEO靠我== DialogResult.OK)

            {

                txtFile.Text = folderBrowserDialog1.SelectedPath;   

            }

        }

————————————————

版权声明:本文为CSDN博SEO靠我主「已被格式化的叔叔」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/sl1990129/article/dSEO靠我etails/79037518
“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2