_____1、用C#怎么样自动检测出在指定目录下(如c:\test)生成文本文件back.txt,c:\test目录有很多子目录,而back.txt文件在各子目录下的bin目录下,如c:\test\
aa\bin\下有一个back.txt,在c:\test\bb\bin下有一个back.txt.
string strFile"c:\test\bb\bin\back.txt";
if(File.Exits(strFile))
   存在
else
  不存在——————————————————2、如果我检测到back.txt文件后,我想取该文件内容中的任意几行数据该怎样取得。如我想取back.txt文件中的第三、第四行内容,该怎样取得?关于这个问题,有一个比较好的方法,就是把文件readline进来,存放到一个arraylist中
arraylist中的第一行就是文件的第一行了

解决方案 »

  1.   

    To brightheroes(SB程序员的悲惨人生) :
    您好,关于我的第一个问题我想您理解错了,我的意思是说我的back.txt文件在c:\test\目录下的子目录中,该子目录名是不确定的,但是back.txt文件是放在该子目录下的bin目录内,如在C:\test\aa\bin\back.txt,问题是aa名是不确定的,而且c:\test下有很多子子目录,如aa、bb、ab...这些目录是动态建立的(该部分已由别人做好)我怎么检测c:\test\不确定的子目录名\bin下有一个back.txt?
    关于我的第二个问题,由于我用C#的时间不是很长,但现在接了一个项目,不完成的话后果...,所以我想如果您可能的话可不可以给我一个代码段,谢谢!
    (找工作真的太难了,我不想失去它,真的)
      

  2.   

    先引用命名空间:
    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
      

  3.   

    对不起, dahuzizyd(你就是我心中的女神) ,用Console.WriteLine(input);我不能得出结果啊,怎么显示啊
      

  4.   

    访问目录可以用递归的方法,给你一个建树的例子,它是从一个目录开始,遍历它所有的子目录,你改一下就好啦:
    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;
      } strDir = theNode.Text + strDir;
     theNode = theNode.Parent;
    }return strDir;} 
     
    关于第二个问题用如下的方法:因为目前没有支持随机读的方法,只有把它读入然后判断,或才边读边判断,用一个计数器:
    string temp="";
    StreamReader reader = null;
    try
    {
    reader = new StreamReader("c:\\log.txt");
    for (string line = reader.ReadLine();line != null;line = reader.ReadLine())
    {
    i++;
    if(i==3)
    {
    temp = temp +line.ToString();

    }
    }
    catch(IOException E)
    {
    MessageBox.Show("读写文件错误");
    e.ToString();
    }
    finally
    {
    if (reader != null)
    reader.Close();
    }MessageBox.Show(temp);
      

  5.   

    To:WYJBCB(阿菜) 
    我的是在控制台程序里写的,你要是在winform下的话,你可以在中断方式下查看输出窗口
      

  6.   

    To snof(雪狼) :
      您好,您的程序我看了,但是我放到类里生成时提示找不到TreeNode的命名空间,经查找它的命名空间是System.Windows.Form,可类中声明没有这个,不知是不是要加一个FOMR到类中,还有我直接在WINFORM中调用(WINFORM可声明System.Windows.Form)它又提示DIR找不到命名空间,
    我该using 什么?谢谢.
    To  dahuzizyd(你就是我心中的女神)
    谢谢您的热心指点!