To snof(雪狼) :
您好,您的程序我看了,但是我放到类里生成时提示找不到TreeNode的命名空间,经查找它的命名空间是System.Windows.Form,可类中声明没有这个,不知是不是要加一个FOMR到类中,还有我直接在WINFORM中调用(WINFORM可声明System.Windows.Form)它又提示DIR找不到命名空间,
我该using 什么?谢谢.
To dahuzizyd(你就是我心中的女神)
谢谢您的热心指点!
访问目录可以用递归的方法,给你一个建树的例子,它是从一个目录开始,遍历它所有的子目录,你改一下就好啦:
protected void PopulateNode(TreeNode aNode)
{
this.Cursor = Cursors.WaitCursor; // Set up the wait cursor
string strDir = BuildDirectory(aNode); // Build the directory tree string from the tree node
Directory Dir = new Directory(strDir); // Use the Directory class to get the files and directory names in the directory
int count = 0;
// Get each directory name and add it as a directory folder tree node
for (int i = 0; i < Dir.GetDirectories().Length; i++)
{
TreeNode ChildNode = new TreeNode(Dir.GetDirectories()[i].Name, 1,0);
aNode.Nodes.Add(i, ChildNode);
count++;
}
// Get each file name and add it as a file tree node
for (int i = 0; i < Dir.GetFiles(m_strFilter).Length; i++)
{
TreeNode ChildNode = new TreeNode(Dir.GetFiles(m_strFilter)[i].Name, 2,2);
aNode.Nodes.Add(i+count, ChildNode);
}
aNode.Expand(); // show the children nodes
this.Cursor = Cursors.Arrow; // restore the cursor
}
The function BuildDirectoryTree is used to construct the string needed to go to the file directory being populated and is shown below:
protected string BuildDirectory(TreeNode aNode)
{
TreeNode theNode = aNode;
string strDir = "";
// Cycle through the node and all its parents to build the full path of the directory
while (theNode != null)
{
if (theNode.Text[theNode.Text.Length - 1] != '\\')
{
strDir = "\\" + strDir;
}
先引用命名空间:
using System.IO
读取文本并输出3.4.5行:
StreamReader sr = File.OpenText("e:\\aa.txt");
String input;
int lineNumber = 0;
while ((input=sr.ReadLine())!=null)
{
lineNumber +=1;
if ( (lineNumber >= 3 ) && (lineNumber <=5) )
{
Console.WriteLine(input);
}
}
Console.WriteLine ("The end of the stream has been reached.");
Console.ReadLine();
}
文本文件:e:\aa.txt;内容:
11111
2222
33333
44444
55555