Searching a XML file form Folders Using Generics

This code demonstrate how to search for a xml file in folder using Generics from dot net 2.0



using System.Collections.Generic;
private string GetSearchPacketReqId(string pathofbasefolder, string filename)
{
try
{
string strFileName = "";
string strReqId = filename + ".xml";
Stack directoryStack = new Stack();
directoryStack.Push(baseDir);

while (directoryStack.Count > 0)
{
string currentDir = directoryStack.Pop();

foreach (string fileNamePath in Directory.GetFiles(currentDir, "*.*"))
{

StringBuilder strBuilder = new StringBuilder();
strBuilder.Append(fileNamePath);
strBuilder.Remove(0, currentDir.Length + 1);
if (strReqId == strBuilder.ToString())
{
//XML file name that you search
strFileName = fileNamePath;
return strFileName;
}
}

foreach (string directoryName in Directory.GetDirectories(currentDir))
{
directoryStack.Push(directoryName);
}
}
return strFileName;
}
catch (Exception ex)
{
throw ex;
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: