ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
添加一个按钮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)