简体   繁体   English

文件夹的C#层次结构

[英]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). 我正在使用Java和C ++,没有使用C#,但是我可以使用它-创建并保存一个XML文件,该文件包含指定驱动器上文件夹的完整层次结构,包括所有短文件的列表(短文件最多8个字符表示名称,最多3个字符作为扩展名)。 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). 我还需要具有字节和字母形式的文件和文件夹的大小,并使用小名称打印文件(小于11个字符,包括扩展名)。 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);
w1.WriteStartElement("Folders");
Console.WriteLine(folders[0]);
Console.WriteLine(files[0]);
Console.WriteLine(folders[1]);
Console.WriteLine(files[1]);
w1.WriteEndElement();
w1.WriteEndDocument();

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

if(fileLegth<=11){
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. 我知道这个主意-制作文件和文件夹,我用数组存储文件,用另一个数组存储文件夹,希望知道如何使它们成为一种XML,然后保存并使用一些函数/循环来查看大小,打印它们等,但是我因为我从未见过这种编程语言,所以感到非常困惑。

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). 我没有使用序列化,所以我选择了一些我熟悉的东西(数组或列表)。

This approach is not the most beneficial one, because what will You do if You have File inside a folder? 这种方法不是最有益的方法,因为如果文件夹中有File,该怎么办? 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 (eg Folders will know about Files inside and vica versa). 从另一个角度解决问题,您可以使用称为OOP的方法-面向对象的编程 ,其中有几个类,您将实例化为对象,并且这些对象将了解其他对象(例如,文件夹将了解内部文件,反之亦然)。

Thankfuly for You, Microsoft has already created this structure in .NET, it can be found in namespace System.IO - link to MSDN . 谢谢您,Microsoft已经在.NET中创建了此结构,可以在名称空间System.IO找到它- 链接到MSDN

For original idea/approach, have a look at https://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx , where is described how to go through all the directiories/files & get info about them. 对于原始想法/方法,请查看https://msdn.microsoft.com/zh-cn/library/dd997370(v=vs.110).aspx ,其中描述了如何遍历所有目录/文件和获取有关它们的信息。

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 ". 即使您不熟悉,.NET也会为大多数类提供XmlSerializer,您只需要创建自己的类即可,该类将告诉Serializer:“ 如何序列化我的类 ”。

Text description of solution: 解决方案的文字说明:

  • Create new class (eg FileTreeManager ) 创建新类(例如FileTreeManager
  • add some properties/variables holding Directories/Files 添加一些保存Directories/Files属性/变量
  • implement interface IXmlSerializable (class have to inherit this so XmlSerializer knows how to serialize) 实现IXmlSerializable接口(类必须继承此接口,因此XmlSerializer知道如何序列化)

Some code to work with (missing XML part): 一些可以使用的代码(缺少XML部分):

public class FileTreeManager
{
    private DirectoryInfo mDirectoryInfo;

    public FileTreeManager(string aEntryPoint)
    {
        SetEntryPoint(aEntryPoint);
    }

    public void SetEntryPoint(string aEntryPoint)
    {
        this.mDirectoryInfo = new DirectoryInfo(aEntryPoint);
    }

    public override string ToString()
    {
        StringBuilder result = new StringBuilder();
        foreach (DirectoryInfo dir in this.mDirectoryInfo.EnumerateDirectories())
        {
            result.Append("|-- " + dir.Name + Environment.NewLine);
        }
        return result.ToString();
    }
}

Your main would look like: 您的主体看起来像:

var ftm = new FileTreeManager("C:\\");
Console.WriteLine(ftm.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM