无法使用c#列出Linux中的所有子目录

时间:2018-12-08 11:00:37

标签: c#

这是我要执行的代码:

this

该程序引用的目录主要包含以下目录列表:

c#_code      Downloads           HelloWorld                   Public
c#_examples  eclipse             Music                        Templates
code         eclipse-workspace   packages-microsoft-prod.deb  tutprog
Desktop      eclipse-workspace2  Pictures                     tut_prog
Documents    examples.desktop    Projects                     Videos

文件PixmapComposite.cs位于c#_examples目录中。这是程序正在寻找的文件。但是,当我打印出实际搜索的目录时,会得到以下列表:

文件:.ICEauthority

主目录:/ home / mark 目录:/home/mark/.cache

文件:event-sound-cache.tdb.3b78ab22329c4ff8b2de7f39f19853b8.x86_64-pc-linux-gnu

主目录:/home/mark/.cache 目录:/home/mark/.cache/.fr-M8r1wi

找不到文件

2 个答案:

答案 0 :(得分:1)

如果您发现与您要查找的文件不匹配的文件条目,则似乎仅在搜索子目录。这意味着,如果目录中没有文件(仅目录),则不会搜索该目录的任何子目录。将搜索子目录的块移到foreach循环之外。另外,只要在返回之前未找到子目录,就需要通过检查null来使子目录搜索继续进行而不返回子目录。

public static DirectoryInfo FindFile( DirectoryInfo dir, FileInfo file_name )
            {
                    FileInfo[] files = dir.GetFiles();
                    foreach( FileInfo file in files) {
                        Console.WriteLine("File: {0}", file.Name);
                        Console.WriteLine();
                        if( file.Name == file_name.Name)
                        {
                             return file.Directory;
                        }
                    }
                    // Search the remaining directories
                    Console.WriteLine("Master dir: {0}", dir.FullName);
                    DirectoryInfo[] sub_dir = dir.GetDirectories();
                    foreach( DirectoryInfo current_dir in sub_dir) {
                         Console.WriteLine("Dir: {0}", current_dir.FullName);
                         Console.WriteLine();
                         var result = FindFile(current_dir, file_name );
                         if (result != null)
                             return result;
                    }
                    return null;
            }

答案 1 :(得分:-1)

看看我今年早些时候写的获取文件的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace WriteFileNamesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();


        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}