C# return full path with GetFiles
Asked Answered
M

6

12

Use this code for search files in directory:

FileInfo[] files = null;
string path = some_path;
DirectoryInfo folder = new DirectoryInfo(path);
files = folder.GetFiles("*.*", SearchOption.AllDirectories);

This return only filename and extension (text.exe). How to return full path to file(C:\bla\bla\bla\text.exe)?

If I use Directory.GetFiles("*.*"), this return full path. But if folder contains point in name(C:\bla\bla\test.0.1), result contains path to folder without file:

  • 0 C:\bla\bla\bla\text.exe
  • 1 C:\bla\bla\test.0.1
  • 2 C:\bla\text.exe

etc.

Mckenzie answered 18/3, 2013 at 13:1 Comment(6)
contains point in name - do you mean a dot .?Fogarty
yep, for example: C:\Windows\winsxs\amd64_1394.inf.resources_31bf3856ad364e35_6.1.7600.16385_en-us_beafdf583b909e3fMckenzie
no, not for example. please explain what you mean exactlyFogarty
@Aschratt DirectoryInfo.GetFiles does return FileInfo[]!Fogarty
@Default: Yes, sorry... I mixed it up with Directory.GetFiles() (msdn.microsoft.com/en-us/library/wz42302f.aspx)Unpremeditated
@Aschratt well, you still got the reputation for it, so it's still a small win for you I guess :) I guess I should blame the approvers for not proofreading...Fogarty
H
31

FileInfo contains a FullName property, which you can use to retrieve full path to a file

var fullNames = files.Select(file => file.FullName).ToArray();

Check

This code on my machine:

FileInfo[] files = null;
string path = @"C:\temp";
DirectoryInfo folder = new DirectoryInfo(path);
files = folder.GetFiles("*.*", SearchOption.AllDirectories);

//you need string from FileInfo to denote full path
IEnumerable<string> fullNames = files.Select(file => file.FullName);

Console.WriteLine ( string.Join(Environment.NewLine, fullNames ) );

prints

C:\temp\1.dot 
C:\temp\1.jpg 
C:\temp\1.png 
C:\temp\1.txt 
C:\temp\2.png 
C:\temp\a.xml 
...

Full solution

The solution to your problem might look like this:

string path = @"C:\temp";
DirectoryInfo folder = new DirectoryInfo(path);
var directories = folder.GetDirectories("*.*", SearchOption.AllDirectories);


IEnumerable<string> directoriesWithDot = 
 directories.Where(dir => dir.Name.Contains("."))
            .Select(dir => dir.FullName);


IEnumerable<string> filesInDirectoriesWithoutDot = 
 directories.Where(dir => !dir.Name.Contains("."))
            .SelectMany(dir => dir.GetFiles("*.*", SearchOption.TopDirectoryOnly))
            .Select(file => file.FullName);


Console.WriteLine ( string.Join(Environment.NewLine, directoriesWithDot.Union(filesInDirectoriesWithoutDot) ) );
Heteropterous answered 18/3, 2013 at 13:4 Comment(7)
@LukeHennerley GetFiles returns a FileInfo[] or am I missing something? msdn.microsoft.com/en-us/library/8he88b63.aspxHeteropterous
@LukeHennerley the files variable is a FileInfo[]Fogarty
@IlyaIvanov If you use a Select projection, you're returning a IEnumerable<string> instead of the original FileInfo array.Lambert
@LukeHennerley this is a static method on DirectoryInfoHeteropterous
@Lambert he needs strings, as he noted at How to return full path to file(C:\bla\bla\bla\text.exe)?Heteropterous
@LukeHennerley: you're wrong, he' using DirectoryInfo, not Directory and GetFiles for DirectoryInfo returns FileInfo[]Misunderstanding
@ClaudioRedi ah yeah, my bad. Didn't clock on to that bit.Euroclydon
S
7

Each FileInfo object has a FullName property.


But if folder contains point in name (C:\bla\bla\test.0.1), result contains path to folder without file

This is an entirely different issue with possibly diffeent answers/workarounds. Can you be more specific?
I cannot reproduce this.

Shoa answered 18/3, 2013 at 13:4 Comment(0)
E
1

You need to use FileInfo.

Directory.GetFiles("", SearchOption.AllDirectories).Select(file => new FileInfo(file).FullName);
Euroclydon answered 18/3, 2013 at 13:5 Comment(0)
W
0
public static IEnumerable<string> GetAllFilesRecursively(string inputFolder)
    {
        var queue = new Queue<string>();
        queue.Enqueue(inputFolder);
        while (queue.Count > 0)
        {
            inputFolder = queue.Dequeue();
            try
            {
                foreach (string subDir in Directory.GetDirectories(inputFolder))
                {
                    queue.Enqueue(subDir);
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("GetAllFilesRecursively: " + ex);
            }
            string[] files = null;
            try
            {
                files = Directory.GetFiles(inputFolder);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("GetAllFilesRecursively: " + ex);
            }
            if (files != null)
            {
                for (int i = 0; i < files.Length; i++)
                {
                    yield return files[i];
                }
            }
        }
    }
Winer answered 18/3, 2013 at 13:6 Comment(0)
S
0

you can try this :

void GetFiles()
    {
        DirectoryInfo d= new DirectoryInfo(strFolderPath);
       //file extension for pdf
        var files = d.GetFiles("*.pdf*");
        FileInfo[] subfileInfo = files.ToArray<FileInfo>();

        if (subfileInfo.Length > 0)
        {
            for (int j = 0; j < subfileInfo.Length; j++)
            {
                bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) & FileAttributes.Hidden) == FileAttributes.Hidden);
                if (!isHidden)
                {
                    string strExtention = th.GetExtension(subfileInfo[j].FullName);
                    if (strExtention.Contains("pdf"))
                    {                            
                        string path = subfileInfo[j].FullName;
                        string name = bfileInfo[j].Name;                           
                    }
                }
            }
        }
Sunday answered 18/3, 2013 at 13:6 Comment(0)
C
0

You can use FileSystemInfo.FullName property.

Gets the full path of the directory or file.

Crispa answered 18/3, 2013 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.