System.IO.Directory.GetFiles() doesn't list all files





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I am using the System.IO.Directory.GetFiles() function to list all executable files from C:WindowsSystem32 with the following code:



using System;
using System.Collections.Generic;
using System.IO;
namespace MyProgram
{
class Program
{
static void Main(string args)
{
string files;
files = System.IO.Directory.GetFiles(@"C:WindowsSystem32", "*.exe");
}
}
}


It lists 300 executables:
enter image description here



But when I list all the files with PowerShell it has 446 executables:
enter image description here



For example: C:WindowsSystem32WinSAT.exe.

It doesn't list this file.

Any idea why it doesn't show me the full list of executable files ? it misses some files.










share|improve this question




















  • 2





    That search you show an image of (Windows Explorer) usually includes sub-folders as well?

    – sellotape
    Nov 24 '18 at 20:50











  • @sellotape oops, yes. I will fix my question.

    – E235
    Nov 24 '18 at 20:53











  • @sellotape I updated my question. Check it again.

    – E235
    Nov 24 '18 at 21:01











  • It might be a permissions issue. Are you running your program with administrative privileges?

    – adam
    Nov 24 '18 at 21:10






  • 1





    Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

    – elgonzo
    Nov 24 '18 at 21:30




















2















I am using the System.IO.Directory.GetFiles() function to list all executable files from C:WindowsSystem32 with the following code:



using System;
using System.Collections.Generic;
using System.IO;
namespace MyProgram
{
class Program
{
static void Main(string args)
{
string files;
files = System.IO.Directory.GetFiles(@"C:WindowsSystem32", "*.exe");
}
}
}


It lists 300 executables:
enter image description here



But when I list all the files with PowerShell it has 446 executables:
enter image description here



For example: C:WindowsSystem32WinSAT.exe.

It doesn't list this file.

Any idea why it doesn't show me the full list of executable files ? it misses some files.










share|improve this question




















  • 2





    That search you show an image of (Windows Explorer) usually includes sub-folders as well?

    – sellotape
    Nov 24 '18 at 20:50











  • @sellotape oops, yes. I will fix my question.

    – E235
    Nov 24 '18 at 20:53











  • @sellotape I updated my question. Check it again.

    – E235
    Nov 24 '18 at 21:01











  • It might be a permissions issue. Are you running your program with administrative privileges?

    – adam
    Nov 24 '18 at 21:10






  • 1





    Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

    – elgonzo
    Nov 24 '18 at 21:30
















2












2








2








I am using the System.IO.Directory.GetFiles() function to list all executable files from C:WindowsSystem32 with the following code:



using System;
using System.Collections.Generic;
using System.IO;
namespace MyProgram
{
class Program
{
static void Main(string args)
{
string files;
files = System.IO.Directory.GetFiles(@"C:WindowsSystem32", "*.exe");
}
}
}


It lists 300 executables:
enter image description here



But when I list all the files with PowerShell it has 446 executables:
enter image description here



For example: C:WindowsSystem32WinSAT.exe.

It doesn't list this file.

Any idea why it doesn't show me the full list of executable files ? it misses some files.










share|improve this question
















I am using the System.IO.Directory.GetFiles() function to list all executable files from C:WindowsSystem32 with the following code:



using System;
using System.Collections.Generic;
using System.IO;
namespace MyProgram
{
class Program
{
static void Main(string args)
{
string files;
files = System.IO.Directory.GetFiles(@"C:WindowsSystem32", "*.exe");
}
}
}


It lists 300 executables:
enter image description here



But when I list all the files with PowerShell it has 446 executables:
enter image description here



For example: C:WindowsSystem32WinSAT.exe.

It doesn't list this file.

Any idea why it doesn't show me the full list of executable files ? it misses some files.







c# getfiles






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 21:00







E235

















asked Nov 24 '18 at 20:44









E235E235

2,17232753




2,17232753








  • 2





    That search you show an image of (Windows Explorer) usually includes sub-folders as well?

    – sellotape
    Nov 24 '18 at 20:50











  • @sellotape oops, yes. I will fix my question.

    – E235
    Nov 24 '18 at 20:53











  • @sellotape I updated my question. Check it again.

    – E235
    Nov 24 '18 at 21:01











  • It might be a permissions issue. Are you running your program with administrative privileges?

    – adam
    Nov 24 '18 at 21:10






  • 1





    Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

    – elgonzo
    Nov 24 '18 at 21:30
















  • 2





    That search you show an image of (Windows Explorer) usually includes sub-folders as well?

    – sellotape
    Nov 24 '18 at 20:50











  • @sellotape oops, yes. I will fix my question.

    – E235
    Nov 24 '18 at 20:53











  • @sellotape I updated my question. Check it again.

    – E235
    Nov 24 '18 at 21:01











  • It might be a permissions issue. Are you running your program with administrative privileges?

    – adam
    Nov 24 '18 at 21:10






  • 1





    Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

    – elgonzo
    Nov 24 '18 at 21:30










2




2





That search you show an image of (Windows Explorer) usually includes sub-folders as well?

– sellotape
Nov 24 '18 at 20:50





That search you show an image of (Windows Explorer) usually includes sub-folders as well?

– sellotape
Nov 24 '18 at 20:50













@sellotape oops, yes. I will fix my question.

– E235
Nov 24 '18 at 20:53





@sellotape oops, yes. I will fix my question.

– E235
Nov 24 '18 at 20:53













@sellotape I updated my question. Check it again.

– E235
Nov 24 '18 at 21:01





@sellotape I updated my question. Check it again.

– E235
Nov 24 '18 at 21:01













It might be a permissions issue. Are you running your program with administrative privileges?

– adam
Nov 24 '18 at 21:10





It might be a permissions issue. Are you running your program with administrative privileges?

– adam
Nov 24 '18 at 21:10




1




1





Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

– elgonzo
Nov 24 '18 at 21:30







Please check if the number of executables provieded by your C# program matches the number of the exe files in the C:WindowsSysWOW64 folder. Your program might be running in 32-bit x86 mode, even if it is set to AnyCPU, due to Visual Studio hosting it in a 32 bit environment.

– elgonzo
Nov 24 '18 at 21:30














2 Answers
2






active

oldest

votes


















0














I guess you are running a 64 bit operating system but try to access this directory running from a C# application compiled to 32 bit. According to Programming Guide for 64-bit Windows / Running 32-bit Applications / File System Redirector
:




The %windir%System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.



In most cases, whenever a 32-bit application attempts to access %windir%System32, %windir%lastgoodsystem32, or %windir%regedit.exe, the access is redirected to an architecture-specific path.



Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%System32 %windir%SysWOW64
%windir%lastgoodsystem32 %windir%lastgoodSysWOW64
%windir%regedit.exe %windir%SysWOW64regedit.exe



So you can either compile your application to x64 or access the missing files via the %windir%SysWOW64 folder.






share|improve this answer
























  • No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

    – elgonzo
    Nov 24 '18 at 21:43













  • @elgonzo what if I want it to search in both system32 and syswow64 ?

    – E235
    Nov 25 '18 at 6:52











  • @E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

    – elgonzo
    Nov 25 '18 at 8:07





















-2














GetFiles will stop when you get an exception. Exception includes access violations. Only way of getting all files is to parse directory by directory using recursion. See code below which writes results to an xml file :



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:temptest.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;

}
}
}





share|improve this answer
























  • Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

    – David Heffernan
    Nov 27 '18 at 4:12












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462208%2fsystem-io-directory-getfiles-doesnt-list-all-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I guess you are running a 64 bit operating system but try to access this directory running from a C# application compiled to 32 bit. According to Programming Guide for 64-bit Windows / Running 32-bit Applications / File System Redirector
:




The %windir%System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.



In most cases, whenever a 32-bit application attempts to access %windir%System32, %windir%lastgoodsystem32, or %windir%regedit.exe, the access is redirected to an architecture-specific path.



Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%System32 %windir%SysWOW64
%windir%lastgoodsystem32 %windir%lastgoodSysWOW64
%windir%regedit.exe %windir%SysWOW64regedit.exe



So you can either compile your application to x64 or access the missing files via the %windir%SysWOW64 folder.






share|improve this answer
























  • No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

    – elgonzo
    Nov 24 '18 at 21:43













  • @elgonzo what if I want it to search in both system32 and syswow64 ?

    – E235
    Nov 25 '18 at 6:52











  • @E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

    – elgonzo
    Nov 25 '18 at 8:07


















0














I guess you are running a 64 bit operating system but try to access this directory running from a C# application compiled to 32 bit. According to Programming Guide for 64-bit Windows / Running 32-bit Applications / File System Redirector
:




The %windir%System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.



In most cases, whenever a 32-bit application attempts to access %windir%System32, %windir%lastgoodsystem32, or %windir%regedit.exe, the access is redirected to an architecture-specific path.



Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%System32 %windir%SysWOW64
%windir%lastgoodsystem32 %windir%lastgoodSysWOW64
%windir%regedit.exe %windir%SysWOW64regedit.exe



So you can either compile your application to x64 or access the missing files via the %windir%SysWOW64 folder.






share|improve this answer
























  • No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

    – elgonzo
    Nov 24 '18 at 21:43













  • @elgonzo what if I want it to search in both system32 and syswow64 ?

    – E235
    Nov 25 '18 at 6:52











  • @E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

    – elgonzo
    Nov 25 '18 at 8:07
















0












0








0







I guess you are running a 64 bit operating system but try to access this directory running from a C# application compiled to 32 bit. According to Programming Guide for 64-bit Windows / Running 32-bit Applications / File System Redirector
:




The %windir%System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.



In most cases, whenever a 32-bit application attempts to access %windir%System32, %windir%lastgoodsystem32, or %windir%regedit.exe, the access is redirected to an architecture-specific path.



Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%System32 %windir%SysWOW64
%windir%lastgoodsystem32 %windir%lastgoodSysWOW64
%windir%regedit.exe %windir%SysWOW64regedit.exe



So you can either compile your application to x64 or access the missing files via the %windir%SysWOW64 folder.






share|improve this answer













I guess you are running a 64 bit operating system but try to access this directory running from a C# application compiled to 32 bit. According to Programming Guide for 64-bit Windows / Running 32-bit Applications / File System Redirector
:




The %windir%System32 directory is reserved for 64-bit applications on 64-bit Windows. Most DLL file names were not changed when 64-bit versions of the DLLs were created, so 32-bit versions of the DLLs are stored in a different directory. WOW64 hides this difference by using a file system redirector.



In most cases, whenever a 32-bit application attempts to access %windir%System32, %windir%lastgoodsystem32, or %windir%regedit.exe, the access is redirected to an architecture-specific path.



Original Path                Redirected Path for 32-bit x86 Processes Redirected
%windir%System32 %windir%SysWOW64
%windir%lastgoodsystem32 %windir%lastgoodSysWOW64
%windir%regedit.exe %windir%SysWOW64regedit.exe



So you can either compile your application to x64 or access the missing files via the %windir%SysWOW64 folder.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 21:40









Markus SafarMarkus Safar

4,82241938




4,82241938













  • No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

    – elgonzo
    Nov 24 '18 at 21:43













  • @elgonzo what if I want it to search in both system32 and syswow64 ?

    – E235
    Nov 25 '18 at 6:52











  • @E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

    – elgonzo
    Nov 25 '18 at 8:07





















  • No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

    – elgonzo
    Nov 24 '18 at 21:43













  • @elgonzo what if I want it to search in both system32 and syswow64 ?

    – E235
    Nov 25 '18 at 6:52











  • @E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

    – elgonzo
    Nov 25 '18 at 8:07



















No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

– elgonzo
Nov 24 '18 at 21:43







No need to compile for x64 if the target is AnyCPU. Just go into the project settings, Build tab and uncheck Prefer 32-bit (assuming x86 execution in the debugger is indeed the reason)

– elgonzo
Nov 24 '18 at 21:43















@elgonzo what if I want it to search in both system32 and syswow64 ?

– E235
Nov 25 '18 at 6:52





@elgonzo what if I want it to search in both system32 and syswow64 ?

– E235
Nov 25 '18 at 6:52













@E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

– elgonzo
Nov 25 '18 at 8:07







@E235, i am not sure i understand your question here. If you want to search both in System32 and in SysWOW64, then search in both directories (assuming you adjusted your project settings as i mentioned in my first comment here). Also be aware that if you run your program on a 32-bit Windows version there is no SysWOW64 folder (it only exists in 64-bit Windows versions).

– elgonzo
Nov 25 '18 at 8:07















-2














GetFiles will stop when you get an exception. Exception includes access violations. Only way of getting all files is to parse directory by directory using recursion. See code below which writes results to an xml file :



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:temptest.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;

}
}
}





share|improve this answer
























  • Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

    – David Heffernan
    Nov 27 '18 at 4:12
















-2














GetFiles will stop when you get an exception. Exception includes access violations. Only way of getting all files is to parse directory by directory using recursion. See code below which writes results to an xml file :



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:temptest.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;

}
}
}





share|improve this answer
























  • Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

    – David Heffernan
    Nov 27 '18 at 4:12














-2












-2








-2







GetFiles will stop when you get an exception. Exception includes access violations. Only way of getting all files is to parse directory by directory using recursion. See code below which writes results to an xml file :



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:temptest.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;

}
}
}





share|improve this answer













GetFiles will stop when you get an exception. Exception includes access violations. Only way of getting all files is to parse directory by directory using recursion. See code below which writes results to an xml file :



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:temptest.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;

}
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 21:10









jdwengjdweng

18.3k2917




18.3k2917













  • Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

    – David Heffernan
    Nov 27 '18 at 4:12



















  • Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

    – David Heffernan
    Nov 27 '18 at 4:12

















Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

– David Heffernan
Nov 27 '18 at 4:12





Nope. This isn't what is going on at all. If there was an exception raised that would be clear in the asker's program.

– David Heffernan
Nov 27 '18 at 4:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462208%2fsystem-io-directory-getfiles-doesnt-list-all-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Xamarin.form Move up view when keyboard appear

JBPM : POST request for execute process go wrong