Tutorial :Overloading Stack.Push?



Question:

I'm trying to add FileInfo details to my xml file by recursively scanning and collecting the files. However, i'm having a bit of trouble and i'm not quite sure how to resolve this issue.

Here's my code:

public static class ScanAndSerialize  {      public static void Serialize()      {          List<string> dirs = FileHelper.GetFilesRecursive(@"s:\project\");              XmlSerializer SerializeObj = new XmlSerializer(dirs.GetType());          string sDay = DateTime.Now.ToString("MMdd");          string fileName = string.Format(@"s:\project\{0}_file.xml", sDay);          TextWriter WriteFileStream = new StreamWriter(fileName);          SerializeObj.Serialize(WriteFileStream, dirs);          WriteFileStream.Close();          }  }    static class FileHelper  {      public static List<string> GetFilesRecursive(string b)      {          List<string> result = new List<string>();            Stack<string> stack = new Stack<string>();          stack.Push(b);            while (stack.Count > 0)          {              string dir = stack.Pop();                try              {                  result.AddRange(Directory.GetFiles(dir, "*.*"));                      foreach (string dn in Directory.GetDirectories(dir))                  {                      DirectoryInfo dirInfo = new DirectoryInfo(@"s:\project\");                      dirInfo.Name.ToString();                      dirInfo.Attributes.ToString();                      dirInfo.CreationTime.ToString();                      dirInfo.Exists.ToString();                      dirInfo.LastAccessTime.ToString();                      dirInfo.LastWriteTime.ToString();                        stack.Push(dn);                                        }                  }              catch              {                }          }          return result;              }    }  

}

Ok where i run into an error is where I'm trying to take the dirInfo.Name.ToString() and the other lines of code and somehow incorporate those into rendering my XML file with these details. The error is that Push cannot be overloaded as i tried:

stack.Push(dn, dirInfo.Name.ToString(), dirInfo.Attributes.ToString().....)  

Anybody have any ideas? Am i doing this backwards?


Solution:1

UPDATE Fixed error

Just use a Stack<DirectoryInfo> instead of a Stack<string>

public static class ScanAndSerialize  {      public static void Serialize()      {          List<string> dirs = FileHelper.GetFilesRecursive(@"s:\project\");          XmlSerializer SerializeObj = new XmlSerializer(dirs.GetType());          string sDay = DateTime.Now.ToString("MMdd");          string fileName = string.Format(@"s:\project\{0}_file.xml", sDay);          TextWriter WriteFileStream = new StreamWriter(fileName);          SerializeObj.Serialize(WriteFileStream, dirs);          WriteFileStream.Close();          }  }    static class FileHelper  {      public static List<string> GetFilesRecursive(string b)      {          List<string> result = new List<string>();            var stack = new Stack<DirectoryInfo>();          stack.Push(new DirectoryInfo (b));            while (stack.Count > 0)          {              var actualDir = stack.Pop();              string dir = actualDir.FullName;                try              {                  result.AddRange(Directory.GetFiles(dir, "*.*"));                      foreach (string dn in Directory.GetDirectories(dir))                  {                      DirectoryInfo dirInfo = new DirectoryInfo(dn);                      dirInfo.Name.ToString();                      dirInfo.Attributes.ToString();                      dirInfo.CreationTime.ToString();                      dirInfo.Exists.ToString();                      dirInfo.LastAccessTime.ToString();                      dirInfo.LastWriteTime.ToString();                        stack.Push(dirInfo);                    }                  }              catch              {                }          }          return result;      }    }  


Solution:2

I'm not sure what all is going on here... First off, Directory.GetFiles can already get all files recursively:

Directory.GetFiles(path, "*.*", SearchOptions.AllDirectories);  

GetDirectories can do the same thing. Judging from your code, your function is named GetFiles but it is getting Directories instead?

Second off, for pushing information onto a stack, why not have a Stack<FileInfo> or Stack<DirectoryInfo>, then manually pop off each one while writing your XML file, reading the information you need and writing it to XML?

Edit: Here's a simpler example. I'm assuming you're trying to get all files from a directory (recursively) and output information about them. I'll combine your two functions together (since a good GetFiles already exists).

using System.Xml;    public static void WriteXMLForAllFiles(string directory, string outputFilePath)  {      XmlWriterSettings settings = new XmlWriterSettings();      settings.Indent = true;        XmlWriter writer = XmlTextWriter.Create(outputFilePath, settings);        writer.WriteStartDocument();      writer.WriteStartElement("Files");        foreach( string file in Directory.GetFiles(directory, "*.*", SearchOptions.AllDirectories) )      {          FileInfo fileInfo = new FileInfo(file);          writer.WriteStartElement("file");            writer.WriteAttributeString("path", file);          writer.WriteAttributeString("creationTime", fileInfo.CreationTimeUtc.ToString());          writer.WriteAttributeString("lastWriteTime", fileInfo.LastWriteTimeUtc.ToString());            writer.WriteEndElement();      }        writer.WriteEndElement();      writer.WriteEndDocument();      writer.Close();  }  

After i looked through your code, you're not using that Stack<> for anything, so ignore that. If you wanted an example in relation to your original code:

public static List<FileInfo> GetFilesRecursive(string b)  {      List<FileInfo> fileList = new List<FileInfo>();        foreach( string file in Directory.GetFiles(directory, "*.*", SearchOptions.AllDirectories) )      {          fileList.Add(new FileInfo(file));      }        return fileList;  }  


Solution:3

Encapsulate the information you're trying to store, then make the Stack out of that:

public class TraversalData {    // All the info you'd like to keep track of.    public string Name;    public string Attributes;    // ...  }    Stack<TraversalData> data = ...    // ...    foreach (string dn in Directory.GetDirectories(dir)) {    // Make a TraversalData out of the information you want to store.    TraversalData td = // ...      data.Push(td);  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »