C#选择文件、选择文件夹、打开文件

一、选择文件用OpenDialoghtml

OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值肯定是否能够选择多个文件
dialog.Title = "请选择文件夹"; dialog.Filter = "全部文件(*.*)|*.*"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string file = dialog.FileName; }
Filter 属性 赋值为一字符串 用于过滤文件类型; 字符串说明以下: ‘|’分割的两个,一个是注释,一个是真的Filter,显示出来的是那个注释。若是要一次显示多中类型的文件,用分号分开。 如: Open1.Filter="图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp"; 则过滤的文件类型为 “|”号  右边的 *.jpg;*.gif;*.bmp 三种类型文件,在OpenDialog/SaveDialog中显示给用户看的文件类型字符串则是 :“|”号  左边的 图片文件(*.jpg,*.gif,*.bmp)。 再如: Open1.Filter="图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png";

二、使用System.Windows.Forms.FolderBrowserDialog选择文件夹async

System.Windows.Forms.FolderBrowserDialog dialog =new System.Windows.Forms.FolderBrowserDialog(); dialog.Description = "请选择Txt所在文件夹"; if (dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK ) { if (string.IsNullOrEmpty(dialog.SelectedPath)) { System.Windows.MessageBox.Show(this, "文件夹路径不能为空", "提示"); return; } this.LoadingText = "处理中..."; this.LoadingDisplay = true; Action<string> a = DaoRuData; a.BeginInvoke(dialog.SelectedPath,asyncCallback, a); }

三、直接打开某路径下的文件或者文件夹ide

System.Diagnostics.Process.Start("ExpLore", "C:\\window");

 

<C#>_在窗体中打开文件

实现的代码以下: public void openfile(int n) { OpenFileDialog openfile = new OpenFileDialog(); openfile.Filter = "*.cs | *.cs";//设置文件后缀
            if (openfile.ShowDialog() == DialogResult.OK) { string filename = openfile.FileName; dic1.Add(n, filename); fileArr[n].Text = filename.Substring(filename.LastIndexOf("\\") + 1, filename.LastIndexOf(".") - (filename.LastIndexOf("\\") + 1)); } } 页面中的【NO】按钮是用来打开文件的,打开的文件是readonly权限,是不可编写的,点击【编辑】按钮就能够打开文件而且编辑,实现代码以下: public void readfile(int btNumber, string mode)//点击【NO】按钮,以只读发方式打开文件
 { int key = Convert.ToInt16(numArr[btNumber].Text) - 1; foreach (KeyValuePair<int, string> kv in dic1) { if (kv.Key == key) { System.IO.FileInfo f = new System.IO.FileInfo(kv.Value); if (mode == "ReadOnly") { f.Attributes = System.IO.FileAttributes.ReadOnly; } System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); } } } public void readfile(int btNumber)//点击【编辑】按钮,以可读可写发方式打开文件
 { int key = Convert.ToInt16(numArr[btNumber].Text) - 1; foreach (KeyValuePair<int, string> kv in dic1) { if (kv.Key == key) { System.IO.FileInfo f = new System.IO.FileInfo(kv.Value); f.Attributes = System.IO.FileAttributes.Normal; System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); } } } 在C#窗体中使用代码实现文件的打开,用的是进程的思想,即Windows中每一个软件都是一个进程,咱们平时在电脑中本身打开一个txt文件就是打开一个进程,在代码中一样能够实现打开文件的功能。 关键语句就是: System.Diagnostics.Process csProcess = System.Diagnostics.Process.Start(kv.Value); 这里的kv.Value是用键值对把文件名和【NO】中的序号对应起来,方便作一些读写操做。 在没有设置文件的权限时,文件是不可改变的,因此以上代码中,若是不实现 f.Attributes = System.IO.FileAttributes.ReadOnly; 文件打开后也是不能更改的,你们能够试试。 为了使文件可以修改,要设置成 f.Attributes = System.IO.FileAttributes.Normal; 设置文件的属性主要用到了FileInfo类的Attributes属性。
View Code

C# OpenFileDialog打开文件对话框(详解)

1、打开文件对话框(OpenFileDialog)

一、 OpenFileDialog控件的基本属性

  • InitialDirectory:对话框的初始目录 
  • Filter: 获取或设置当前文件名筛选器字符串,例如,"文本文件(*.txt)|*.txt|全部文件(*.*)||*.*" 
  • FilterIndex 在对话框中选择的文件筛选器的索引,若是选第一项就设为1 
  • RestoreDirectory 控制对话框在关闭以前是否恢复当前目录 
  • FileName:第一个在对话框中显示的文件或最后一个选取的文件 
  • Title 将显示在对话框标题栏中的字符 
  • AddExtension 是否自动添加默认扩展名 
  • CheckPathExists 在对话框返回以前,检查指定路径是否存在 
  • DefaultExt 默认扩展名 
  • DereferenceLinks 在从对话框返回前是否取消引用快捷方式 
  • ShowHelp 启用"帮助"按钮 
  • ValiDateNames 控制对话框检查文件名中是否不含有无效的字符或序列

二、 OpenFileDialog控件有如下经常使用事件

FileOk 当用户点击"打开"或"保存"按钮时要处理的事件 
HelpRequest 当用户点击"帮助"按钮时要处理的事件post

能够用如下代码来实现上面这个对话框:this

private void openFileDialogBTN_Click(object sender, System.EventArgs e) { OpenFileDialog openFileDialog=new OpenFileDialog(); openFileDialog.InitialDirectory="c:\\";//注意这里写路径时要用c:\\而不是c:\
    openFileDialog.Filter="文本文件|*.*|C#文件|*.cs|全部文件|*.*"; openFileDialog.RestoreDirectory=true; openFileDialog.FilterIndex=1; if (openFileDialog.ShowDialog()==DialogResult.OK) { fName=openFileDialog.FileName; File fileOpen=new File(fName); isFileHaveName=true; richTextBox1.Text=fileOpen.ReadFile(); richTextBox1.AppendText(""); } }
View Code

三、 获取对话框的文件名

openfiledialog.FileName //获取或设置一个包含在文件对话框中选定的文件名字符串spa

openfiledialog.SafeFileName  //获取选定对话框中的文件名和扩展名code

2、打开文件夹对话框(FolderBrowserDialog)

FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == DialogResult.OK) { savePath = dialog.SelectedPath; textBox2.Text = savePath; }
View Code
相关文章
相关标签/搜索