I want to search for xml files through out a folder hierarchy and make a not of them?
That means you need to iterate trought a directory tree an find the xml file. To go through a hierachical folder you can refer to the frist reply or these sample code below:
static void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
System.IO.FileInfo[] files = null;
System.IO.DirectoryInfo[] subDirs = null;
// First, process all the files directly under this folder
try
{
files = root.GetFiles("*.*");
}
// This is thrown if even one of the files requires permissions greater
// than the application provides.
catch (UnauthorizedAccessException e)
{
// This code just writes out the message and continues to recurse.
// You may decide to do something different here. For example, you
// can try to elevate your privileges and access the file again.
log.Add(e.Message);
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
}
if (files != null)
{
foreach (System.IO.FileInfo fi in files)
{
// In this example, we only access the existing FileInfo object. If we
// want to open, delete or modify the file, then
// a try-catch block is required here to handle the case
// where the file has been deleted since the call to TraverseTree().
Console.WriteLine(fi.FullName);
}
// Now find all the subdirectories under this directory.
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
// Resursive call for each subdirectory.
WalkDirectoryTree(dirInfo);
}
}
}
To find xml fine you need to use Directory.GetFiles method or DiretoryInfo.GetFile methods. The sample code above uses DirectoryInfo.Getfiles method:
//Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; // Will Retrieve count of all files in directry and sub directries
//Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length; // Will Retrieve count of all files in directry but not sub directries
//Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length; // Will Retrieve count of files XML extension in directry and sub directries
DirectoryInfo di = new DirectoryInfo(@"c:\");
// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*.xml");
aquarius24
Member
19 Points
18 Posts
Browisng through folder hierarchy
May 03, 2012 08:53 AM|LINK
Hi All
I want to search for xml files through out a folder hierarchy and make a not of them?
Can anyone help me out on this?
r.rajeshkhun...
Participant
1358 Points
250 Posts
Re: Browisng through folder hierarchy
May 03, 2012 09:12 AM|LINK
Hi,
Check below links, it will help you...
http://msdn.microsoft.com/en-us/library/bb513869.aspx
======================================
Microsoft® Community Contributor 2011 | AWARD
MCTS
My Blog
Follow Me
hrishirocks
Member
60 Points
17 Posts
Re: Browisng through folder hierarchy
May 03, 2012 09:50 AM|LINK
Take a look at this post.You would need to edit it a little and i think you would be good with it.
http://www.aspnettutorials.com/tutorials/file/checkexistence-csharp.aspx
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: Browisng through folder hierarchy
May 10, 2012 07:56 AM|LINK
Hi,
That means you need to iterate trought a directory tree an find the xml file. To go through a hierachical folder you can refer to the frist reply or these sample code below:
static void WalkDirectoryTree(System.IO.DirectoryInfo root) { System.IO.FileInfo[] files = null; System.IO.DirectoryInfo[] subDirs = null; // First, process all the files directly under this folder try { files = root.GetFiles("*.*"); } // This is thrown if even one of the files requires permissions greater // than the application provides. catch (UnauthorizedAccessException e) { // This code just writes out the message and continues to recurse. // You may decide to do something different here. For example, you // can try to elevate your privileges and access the file again. log.Add(e.Message); } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine(e.Message); } if (files != null) { foreach (System.IO.FileInfo fi in files) { // In this example, we only access the existing FileInfo object. If we // want to open, delete or modify the file, then // a try-catch block is required here to handle the case // where the file has been deleted since the call to TraverseTree(). Console.WriteLine(fi.FullName); } // Now find all the subdirectories under this directory. subDirs = root.GetDirectories(); foreach (System.IO.DirectoryInfo dirInfo in subDirs) { // Resursive call for each subdirectory. WalkDirectoryTree(dirInfo); } } }To find xml fine you need to use Directory.GetFiles method or DiretoryInfo.GetFile methods. The sample code above uses DirectoryInfo.Getfiles method:
//Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; // Will Retrieve count of all files in directry and sub directries //Directory.GetFiles(path, "*.*", SearchOption.TopDirectory).Length; // Will Retrieve count of all files in directry but not sub directries //Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length; // Will Retrieve count of files XML extension in directry and sub directries DirectoryInfo di = new DirectoryInfo(@"c:\"); // Get only subdirectories that contain the letter "p." DirectoryInfo[] dirs = di.GetDirectories("*.xml");http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.getfiles(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles.aspx
Feedback to us
Develop and promote your apps in Windows Store