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

时间:2016-08-29 16:26:48

标签: c#

我有字符串列表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];
    }

我想以这种方式分组和显示列表http://pastebin.com/aML6V3uV 怎么做?

评论行来自我以前的测试。

我还写了提取路径的函数

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

1 个答案:

答案 0 :(得分:0)

我有这个旧功能。可以帮助你。我做了你想要的事情。按文件夹分组一些文件。

我在我的班级中将m_Files变量定义为:

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

如果我在我的班级中有这个功能(例如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);
    }

我将这个函数称为:

PopulateList(rootDirectory);

然后它用我的m_File字典填充这个根目录中的所有jpg文件及其sub_directories递归。显然你可以为它创建更多参数,如文件扩展名或文件的其他属性。例如文件名的一部分。