C#基础学习之二 文件读取 denuvead
2011-11-18 21:06
363 查看
/article/9472908.html
1.一个button 命名为 btok
2.一个textbox 命名为 tbInput
3.一个Listbox 命名为 lbResult
ok 我们分别为button和listbox建立事件来实现显示textbox中路径下文件和listbox中选中文件的部分属性
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace complete
{
public partial class Form1 : Form
{
string currentpath;
public Form1()
{
InitializeComponent();
}
private void btOK_Click(object sender, EventArgs e)
{
//从TextBox中提取输入的字符
string path = tbInput.Text;
//检查路径名是否合法
if (path.Length > 0)
{
if (Directory.Exists(path))
{
lbResult.Items.Clear();
//获取目录中的所有文件名列表
string[] files = Directory.GetFiles(path);
//将文件去掉路径名放在列表框中
foreach (string file in files)
{
FileAttributes attr = File.GetAttributes(file);
if ((attr & FileAttributes.Hidden) == 0)
lbResult.Items.Add(Path.GetFileName(file));
}
//在读者双击一个文件名的情况下保存路径名
currentpath = Path.GetFullPath(tbInput.Text);
}
//如果路径无效,则告知读者
else
MessageBox.Show(path+" is not a valid path","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void lbResult_DoubleClick(object sender, EventArgs e)
{
//根据读者双击的文件名创建一个完全限定的文件名
string file = currentpath;
if (!file.EndsWith(":") && !file.EndsWith("//"))
file += "/";
file += lbResult.SelectedItem.ToString();
//显示文件创建时间和最后一次修改的时间
DateTime created = File.GetCreationTime(file);
DateTime modified = File.GetLastWriteTime(file);
string msg = "创建时间:" + created.ToLongDateString() + " 于:" + created.ToLongTimeString() + " " + "修改:" + modified.ToLongDateString() + " 于:" + modified.ToLongTimeString();
MessageBox.Show(msg, lbResult.SelectedItem.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
1.一个button 命名为 btok
2.一个textbox 命名为 tbInput
3.一个Listbox 命名为 lbResult
ok 我们分别为button和listbox建立事件来实现显示textbox中路径下文件和listbox中选中文件的部分属性
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace complete
{
public partial class Form1 : Form
{
string currentpath;
public Form1()
{
InitializeComponent();
}
private void btOK_Click(object sender, EventArgs e)
{
//从TextBox中提取输入的字符
string path = tbInput.Text;
//检查路径名是否合法
if (path.Length > 0)
{
if (Directory.Exists(path))
{
lbResult.Items.Clear();
//获取目录中的所有文件名列表
string[] files = Directory.GetFiles(path);
//将文件去掉路径名放在列表框中
foreach (string file in files)
{
FileAttributes attr = File.GetAttributes(file);
if ((attr & FileAttributes.Hidden) == 0)
lbResult.Items.Add(Path.GetFileName(file));
}
//在读者双击一个文件名的情况下保存路径名
currentpath = Path.GetFullPath(tbInput.Text);
}
//如果路径无效,则告知读者
else
MessageBox.Show(path+" is not a valid path","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void lbResult_DoubleClick(object sender, EventArgs e)
{
//根据读者双击的文件名创建一个完全限定的文件名
string file = currentpath;
if (!file.EndsWith(":") && !file.EndsWith("//"))
file += "/";
file += lbResult.SelectedItem.ToString();
//显示文件创建时间和最后一次修改的时间
DateTime created = File.GetCreationTime(file);
DateTime modified = File.GetLastWriteTime(file);
string msg = "创建时间:" + created.ToLongDateString() + " 于:" + created.ToLongTimeString() + " " + "修改:" + modified.ToLongDateString() + " 于:" + modified.ToLongTimeString();
MessageBox.Show(msg, lbResult.SelectedItem.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
相关文章推荐
- C#基础学习之二 文件读取
- Java基础学习总结(15)——java读取properties文件总结
- C#源码学习之---异步读取文件
- 黑马程序员--Java基础学习之IO流之字节流、字符流、读取写入文件、Copy文件、键盘输入输出、流操作的基本规律
- 依次读取文件中的一个字符 分类: python基础学习 python 小练习 2013-08-22 11:14 289人阅读 评论(0) 收藏
- Java基础学习总结(15)——java读取properties文件总结
- c#学习之基础篇(Windows应用程序 文件操作)
- C语言文件的读入与写入及C#基础函数学习[图]
- C#学习笔记:从文件中读取数据
- C#学习笔记(二十一):使用文件基础
- Python 基础学习---读取json文件
- C#笔记 通用文件的读取之二——XML文件的读取与写入
- git学习之二:文件控制基础
- .NET学习技巧:C# 配置文件读取与修改
- java===java基础学习(5)---文件读取,写入操作
- 黑马程序员之C#学习笔记:C#读取txt文件的方法
- 【Java学习笔记】基础知识学习17【文件文本读取写入】
- opencv学习---Directory类基础知识介绍(读取文件夹下的所有文件的文件名)
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- Java学习基础组件---Properties文件的读取