简体   繁体   English

如何分组和显示字符串列表

[英]How to group and display list of strings

I have list of strings http://pastebin.com/upTpYUTT I generated this list using this code 我有字符串列表http://pastebin.com/upTpYUTT我使用此代码生成了此列表

    public class tdata
    {
        public string fpath { get; set; }
        public string fsize { get; set; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TorrentFile torrent = Bencode.DecodeTorrentFile("mik.torrent");
        /*BString announce = (BString)torrent["encoding"];
        object dat = (object)torrent["creation date"];
        richTextBox1.SelectionFont = new Font("Tahoma", 11, FontStyle.Regular);

        BDictionary info = torrent.Info;

        string mk = info["length"].ToString();
        long size = Convert.ToInt64(mk);*/

        //richTextBox1.Text = lista.Count.ToString();


        List<tdata> name = new List<tdata>();

        BList files = (BList)torrent.Info["files"];
        foreach (BDictionary file in files)
        {
            // File size in bytes (BNumber has implicit conversion to int and long)
            int size = (BNumber)file["length"];

            // List of all parts of the file path. 'dir1/dir2/file.ext' => dir1, dir2 and file.ext
            BList path = (BList)file["path"];

            //int i = 0;

            String filepath = String.Empty;

            foreach (IBObject f in path)
            {
                filepath += @"\" + f.ToString();
            }

            if (filepath.Substring(0,1) == @"\")
            {
                filepath = filepath.Substring(1);
            }

            tdata san = new tdata();
            san.fpath = filepath;
            san.fsize = BytesToString(size);

            name.Add(san);
        }

        foreach (tdata mak in name)
        {
            richTextBox1.SelectionFont = new Font("Tahoma", 9, FontStyle.Regular);
            richTextBox1.AppendText("-> " + mak.fpath + "\r\n");
            richTextBox1.SelectionFont = new Font("Tahoma", 9, FontStyle.Regular);
            richTextBox1.AppendText("--> " + mak.fsize + "\r\n");
        }

    }

    static String BytesToString(long byteCount)
    {
        string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
        if (byteCount == 0)
            return "0" + suf[0];
        long bytes = Math.Abs(byteCount);
        int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
        double num = Math.Round(bytes / Math.Pow(1024, place), 2);
        return (Math.Sign(byteCount) * num).ToString() + " " +suf[place];
    }

I want to group and display list in this way http://pastebin.com/aML6V3uV How to do this ? 我想以这种方式对列表进行分组和显示http://pastebin.com/aML6V3uV如何执行此操作?

Comment lines are from my previous tests. 评论行来自我之前的测试。

I also writed function to extract path 我还写了提取路径的函数

    string path (string data)
    {
        string[] p = data.Split('\\');

        StringBuilder path = new StringBuilder();

        for (int i=0; i < p.Length - 1; i++)
        {
            if (i > 0)
                path.Append("\\");
            path.Append(p[i]);
        }
        path.Append("\\");

        return path.ToString();
    }

I have this old function .maybe it can help you.I have done something like you wanted.Grouping some files by their folder. 我有这个旧功能。也许它可以为您提供帮助。我已经完成了您想要的操作。按文件夹将某些文件分组。

I defined m_Files variable in my class as : 我在类中将m_Files变量定义为:

        Dictionary<string, List<string>> m_Files = new Dictionary<string, List<string>>();

If I have this function in my class(for example System.Windows.Form.Form) 如果我的班级中有此功能(例如System.Windows.Form.Form)

       private void PopulateList(string path)
    {
        List<string> files = new List<string>();
        foreach (string str in System.IO.Directory.GetFiles(path, "*.jpg"))
            files.Add(str);
        if (files.Count > 0)
            this.m_Files.Add(path, files);
        foreach (string str in System.IO.Directory.GetDirectories(path))
            this.PopulateList(str);
    }

and I call this function like this: 我这样调用此函数:

PopulateList(rootDirectory);

then it fills my m_File dictionary with all jpg files inside this root directory and its sub_directories recursively.Obviously you can make more parameters for it like file extension or other properties of file.For example part of file name. 然后它会以递归方式将根目录中所有jpg文件及其子目录中的所有jpg文件填充到我的m_File字典中。显然,您可以为其设置更多参数,例如文件扩展名或文件的其他属性。例如文件名的一部分。

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

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