日期:2014-05-17 浏览次数:21325 次
//文件位置
string path = this.txtFilePath.Text;
if (string.IsNullOrEmpty(path))
{
MessageBox.Show("文件位置必须填写");
return;
}
//创建读取流
StreamReader sr = new StreamReader(path);
try
{
显示的控件.Text = sr.ReadToEnd();
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
sr.Close();
------解决方案--------------------
//文件位置
string path = this.txtFilePath.Text;
if (string.IsNullOrEmpty(path))
{
MessageBox.Show("文件位置必须填写");
return;
}
//创建读取流
StreamReader sr = new StreamReader(path);
try
{
显示的控件.Text = sr.ReadToEnd();
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
sr.Close();
------解决方案--------------------
string path = @"C:\\";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles(path))
{
if (file.Extension.Equals("txt"))
{
string filePath = file.FullName;
string txtContent = File.ReadAllText(filePath, Encoding.GetEncoding("GB2312"));//读取文件
}
}
------解决方案--------------------
string path = @"C:\\";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles())
{
if (file.Extension.Equals(".txt"))//如果是txt文件
{
string filePath = file.FullName;
string txtContent = File.ReadAllText(filePath, Encoding.GetEncoding("GB2312"));//读取文件
if (txtContent.Contains("hello"))
{
//如果文件内容中包含hello
}
}
}
------解决方案--------------------
呵呵,这你算问对人了,我喜欢一行一行的读,代码清晰简单,然后自己转换,我把我的经验心得分给你吧!
if (File.Exists("F:\\图书1.txt"))
{
string[] str = File.ReadAllLines("F:\\图书1.txt");
}
这样一个TXT就存在一个STR字符串数组里了,str[0]是第一行数据,一次类推。
有人问这样的话一行一行我想让他们分开啊 不想这样比如说我想把数据导入到LISTVIEW里面 怎么办。嘿嘿 你又找对人啦!
for (int i = 1; i < str.Length; i++)
{
char[] ch = new char[]{' '};
string[] row = str[i].Split(ch,StringSplitOptions.RemoveEmptyEntries);
listView1.Items.Add(new ListViewItem (row));
}