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

本文关键字:显示 字符串 列表 | 更新日期: 2023-09-27 18:04:47

我有一个字符串列表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();
    }

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

我有这个旧的函数,也许它可以帮助你。我做了你想做的事。按文件夹对文件进行分组

我定义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);

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