添加一个按钮button,添加OpenFileDialog控件。
判断对话框是否选中:
```
//双击button按钮
private void btn_选择文件_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
FileInfo fInfo = new FileInfo(openFileDialog1.FileName);
string strname = fInfo.Name;
string strcreattime = fInfo.CreationTime.ToLongDateString();
MessageBox.Show(strname + strcreattime);
}
}
```
# 实现 按回车就确定
在窗体load事件中加入以下代码即可:
```
this.AcceptButton = btn_Login; //这个btn_Login是按钮的名字
```
# 文件
1. file 类
是静态方法,可以直接后面跟一个点来调用
直接用 file.open等可以执行相关操作
```
file.exist("c:\\text.txt") //即可判断文件是否存在。
```
2. fileinfo 类
需要创建对象以后,通过对象调用
```
FileInfo finfo = new FileInfo();
finfo.new("c:\\Test.txt");
```
3. 创建文件
file.create("路径") 方法,或者是fileinfo对象的 fileinfo.create("路径")属性
4. 复制文件
`file.copy;`
`fileinfo.copyto;`
5. 移动文件
`file.move;`
`fileinfo.moveto;`
6. 获取文件信息
```
file.name; //获取文件名
file.fullname; //获取文件完整目录,包括文件名,一般用 fullname 来获取(或者说)选中文件
file.directoryName; //获取文件完整路径
file.IsReadOnly; //获取文件是否只读
file.CreationTime; // 获取文件创建时间
file.Length; // 获取文件大小
```
![](https://box.kancloud.cn/d95746a077102c48baf6aa14b4eeb72c_459x268.png)
> 判断是否选择了文件:用openfiledialog控件;
> 判断是否选择了文件夹:用folderBrowserDialog1控件;
判断文件是否存在,并且用已经选择的文件创建 fileInfo 对象:
用openfiledialog的 filename 来获取显示的文件,并且显示在文本框里面,**所以后面创建对象的时候,括号中要用到textbox1.Text,而不是textbox1**
```
if (openFileDialog1.ShowDialog() == DialogResult.OK ) // 直接用openfiledialog控件来操作
{
textBox1.Text = openFileDialog1.FileName;
FileInfo finfo = new FileInfo(textBox1.Text);
string name = finfo.Name;
string path = finfo.FullName;
}
```
# 文件夹
directory 类
directoryInfo 类
同file+fileinfo 类
> 1. 但文件夹的 move 移动,不能在不同的磁盘之间移动(比如从 c 盘移动到 D盘是不可以的)
>
> 2. 判断是否选择了文件夹:用folderBrowserDialog1控件;
常用方法:
getFilesystemInfo 方法:用来获取文件夹下所有子文件夹和文件
![](https://box.kancloud.cn/414e0c6cf21da6bca7e7a7c615f4959c_603x259.png)
## 遍历文件夹
打开文件用的是openfileDialog 控件。
打开文件夹用的是 folderBrowserDialog 控件。控件属性 view 设置为 details。
获取文件夹:
`folderBrowserDialog.selectedPath` //一般用 selectedPath 来获取(或者说选中)文件夹
directory.getfilesystemInfo方法,可以用来获取文件夹下的所有子文件夹和文件。
## Getfiles
但传回的是file SystemInfo 的数组,所以必须定义fileSystemInfo的数组来接收。
![](https://box.kancloud.cn/3dc1dbc3b7e7153ed7f91f52c5dbc0d2_812x432.png)
```
if(folderbrowserdialog.showdialog == dialogResult.ok)
{
textbox1.Text = folderbrowserdialog.selectedPaht;
DirectoryInfo info = new directoryInfo(textbox1.Text);
FiresystemInfo [] finfos = info.getfileSystemInfos;
foreach( FilesystemInfo f in finfos) // 定义一个变量用来表示 finfos 数组里面的元素。因为类型是 filesysteminfo 类型,所以用FilesystemInfo f
{
if(f is DirectoryInfo) //如果是文件夹
{
DirectoryInfo dir = new DirectoryInfo(f.FullName);
listView1.Items.Add(dir.Name); // 添加到listview控件中
listView1.Items\[listView1.Items.Count - 1\].SubItems.Add(dir.FullName);
}
else //如果是个文件
{
FileInfo file = new FileInfo(f.FullName);
listView1.Items.Add(file.Name);
listView1.Items\[listView1.Items.Count - 1\].SubItems.Add(file.FullName);
}
}
}
```
![](https://box.kancloud.cn/3754d96764d65d064a0c5ec257b3b50d_736x474.png)
# 如何对打开的文件进行读写操作
fileStream 类表示。
![](https://box.kancloud.cn/50fc89c8d4822c28029bd2c6b512093c_819x228.png)
## 属性
![](https://box.kancloud.cn/0f5d81380e8e20b388d32a5e384c9b15_827x288.png)
## 方法
![](https://box.kancloud.cn/8aaed106740185a62d8d1c6aa35538f3_574x266.png)
- 帮助文档 microsoft helo viewer
- c#开发环境及visual studio安装注意事项
- c#程序基本结构-基本语法
- Q1: public static void main(String[] args) 是什么意思
- Q2: c#命名空间+Main方法
- Q3:注释+命名规则+代码规则
- Q4: c#语句 system => console
- Q5: 数据类型 .net
- Q5: 常用名字、变量、运算符
- Q6: 对话窗输入-属性
- Q7: 递归
- Q8:决策分支、条件判断语句 if 语句
- Q9:数组
- Q10:字符串
- Q11:对象、类、访问权限、静态动态函数
- Q12:方法及参数——继承于类
- Q13:构造函数
- Q14:继承——base 关键字
- Q15:多态、虚方法、接口
- Q16:创建窗体应用、控件
- Q17:Ado数据访问、连接 sqlserver 数据库
- Q18: 读取数据command + DataRead( )、DataSet + DateAdapter
- Q19: Entity Framwork、entity 与 ADO.net的区别
- Q20: 对话框、文件、文件夹
- Q21: 导入excel数据、更新到 dbo 数据库中
- Q26: 获取 excel 中每个 sheet 的表名
- Q22: 两个窗体之间数据+方法传递
- Q23: 数学对象
- Q24: c#网站编写
- Q25: visual studio2017如何查看帮助
- Q27: c# dictionary 字典对象
- Q28: 数组与dataTable互相转化