这个并不复杂 FileInfo 类操作文件 改名删除操作StreamReader 来读取txt 文件using System; using System.IO;class Test { public static void Main() { try { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader("TestFile.txt")) { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // Let the user know what went wrong. Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } } }
FileInfo 类操作文件 改名删除操作StreamReader 来读取txt 文件using System;
using System.IO;class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
/// <summary>
/// 此方法映射磁盘
/// </summary>
public void reflectDriver()
{
DriveInfo[] dr = DriveInfo.GetDrives();
string driveName = "";
foreach (DriveInfo d in dr)
{
switch (d.DriveType)
{
case DriveType.Fixed:
driveName = "本地磁盘(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.Removable:
driveName = "可移动磁盘(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.CDRom:
driveName = "DVD驱动器(" + d.Name.Substring(0, 2) + ")";
break;
case DriveType.Network:
driveName = "网络驱动器(" + d.Name.Substring(0, 2) + ")";
break;
default:
driveName = "未知(" + d.Name + ")";
break;
}
this.treeView1.Nodes.Add(d.Name, driveName);
}
}
//调用
private void button6_Click(object sender, EventArgs e)
{
reflectDriver();
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.IsExpanded)
{
e.Node.Collapse(); }
else
{
e.Node.Expand();
}
}
else
{
if (Directory.Exists(e.Node.Name))
{
try
{
string[] allDirectory = Directory.GetDirectories(e.Node.Name);
foreach (string s in allDirectory)
{
e.Node.Nodes.Add(s, s.Remove(0, s.LastIndexOf("\\") + 1));
}
listBox1.Items.Clear();
string[] allFiles = Directory.GetFiles(e.Node.Name);
foreach (string sf in allFiles)
{
//listBox1.Items.Add(sf.Remove(0, sf.LastIndexOf("\\") + 1));
listBox1.Items.Add(sf);
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}
}
catch
{
}
}
e.Node.Expand();
}
} void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = listBox1.Items[1].ToString();
StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));//读取文件 string str = null;
while ((str = sr.ReadLine()) != null)//判断行
{
listBox1.Items.Add(str);
}
}