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; } }