Ana Ana - 1 year ago 143
C# Question

C# hierarchy of folders

I am trying to do some project and get stuck with it. I was doing Java and C++, no C#, but I got this to work on - create and save an XML file containing complete hierarchy of folders on a specified drive including a list of all files that are short (short file has up to 8 characters for name and up to 3 characters for extension). I also need to have size of file and folder in bytes and in letters and print files with small name (smaller then 11 char including extension). This is my code:

using System;
using System.IO;
using System.Xml;
using System.Linq;

public class Program{
public static void Main(string[] args)
//Your code goes here

string[] folders = {"Folder1", "Folder2"};
string[] files = {"File1.txt", "File2.txt"};
XmlTextWriter w1 = new XmlTextWriter("filesandfolders.xml", System.Text.Encoding.UTF8);

File.WriteAllText("filesandfolders.xml", "structure of folders and files");

long fileSize = 0;
int fileLegth = 0;
int NumberOfShortFiles;

Console.WriteLine("Folder names are: [{0}]", string.Join(", ", folders));
Console.WriteLine("File names are: [{0}]", string.Join(", ", files));

for(long p=0; p<=files.Length; p++){

Console.WriteLine("Size of this file is "+fileSize);

for(int q=0; q<=files.Length; q++){

Console.WriteLine("Directory length is "+q);

for(int m=0; m<=11; m++){
NumberOfShortFiles = m;

Console.WriteLine("File name is short.");
else{Console.WriteLine("File name is short or not evaluated.");}


I know the idea - make files and folders, I used array for files and another for folders hoping to know how to make them to be one XML, then save it and use some functions/loops to see size, print them etc, but I am quite confused since I never saw this kind of programming language earlier.

Let me say that for now problem is that I have size in bytes, but don't have number of letters, and I am missing root element (that is what compiler said). I didn't work with serialization, so I took something that I am familiar with (array or list).

Answer Source

This approach is not the most beneficial one, because what will You do if You have File inside a folder? And next to it there is other folder with another file?

|-- Folder1
|   |- File1
|   |- File2
|-- Folder2
|   |- File3
|   |- File4

Taking the problem from other side, You might use something called OOP - Object Oriented Programming, where You have several classes, You will instanciated into objects and the objects will know about other objects (e.g. Folders will know about Files inside and vica versa).

Thankfuly for You, Microsoft has already created this structure in .NET, it can be found in namespace System.IO- link to MSDN.

For original idea/approach, have a look at , where is described how to go through all the directiories/files & get info about them.

Once You have this structure of classes (Files/Directories), it is time to move to Serialization. Even You are unfamiliar with this, .NET provides XmlSerializer for most of classes and You just need to create Your own class, which will tell the Serializer, "How to serialize my class".

Text description of solution:

  • Create new class (e.g. FileTreeManager)
  • add some attributes/variables holding Directories/Files
  • implement XmlSerializer (class have to inherit from XmlSerializer)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download